Search code examples
ioscloudkit

How to initialize CKRecord with both zoneID and recordID?


I'm currently building a solution to sync Core Data records to CloudKit.

I need help to find out it what I want to do with CKRecord is feasible.

I’ve checked the Apple CloudKit documentation, experiment and searched the web, but I didn’t find the kind of answer I want.

I would like to initialize the CKRecord with having zoneID and also recordID that I would supply in init method.
The initializer seems to force me to choose between the two. I want to create my own ZoneID (not using default), and also set the recordID instead of having it being autogenerated.

Would that be possible?


Solution

  • You can specify both a zone ID and a record ID, but you need to do it in three steps:

    First, create a CKRecordZoneID with your zone ID and user:

    let ckRecordZoneID = CKRecordZoneID(zoneName: "myZone", ownerName: CKOwnerDefaultName)
    

    Then you can created a CKRecordID with your required record ID and specifying your CKRecordZoneID:

    let ckRecordID = CKRecordID(recordName: recordIDString, zoneID: ckRecordZoneID)
    

    Finally you can create the CKRecord using that record ID:

    let ckRecord = CKRecord(recordType: myRecordType, recordID: ckRecordID)