Bartłomiej Semańczyk
Bartłomiej Semańczyk

Reputation: 61880

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?

Upvotes: 3

Views: 329

Answers (1)

Bartłomiej Semańczyk
Bartłomiej Semańczyk

Reputation: 61880

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.

Upvotes: 3

Related Questions