ioscloudkit

How to migrate ckrecords from default zone to custom one?


I need to migrate the records because 'CKFetchRecordChangesOperation' doesn't work for the default one. Should I migrate it one by one, or is there any way to do it automatically?


Solution

  • Yes, it needs to be done one by one. You need to fetch all from old zone, and then CREATE a new ones for new custom zone like this:

    func newRecord(from record: CKRecord, newZone: CKRecordZone) -> CKRecord {
    
        let recordID = CKRecordID(recordName: record.recordID.recordName, zoneID: newZone.zoneID)
        let newRecord = CKRecord(recordType: "Goal", recordID: recordID)
    
        newRecord["startDate"] = record["startDate"]
        newRecord["endDate"] = record["endDate"]
        newRecord["hours"] = record["hours"]
    
        return newRecord
    }
    

    and save to CKDatabase.