12 Replies
      Latest reply on May 14, 2020 6:08 AM by fsdolphin
      dsbirch Level 1 Level 1 (0 points)

        I'm wondering if it's possible to save a new CKRecord to a custom zone and have iCloud automatically generate a record ID. I have my zones set up as I'd like. But if I save a new record to iCloud with the CKDatabase.save method, it saves it to the default zone, which is not where I want to save it. If I use a CKModifyRecordsOperation to save the record to its custom zone, it appears I first need to fully construct a CKRecord including a pre-defined record ID, and iCloud uses that record ID rather than generating one. Is there any way of achieving both of my goals other than auto-generating my own unique ID for each record?

        • Re: New CKRecord in custom zone
          PBK Level 7 Level 7 (3,795 points)

          Can't you construct the record without specifying a recordID?

          • Re: New CKRecord in custom zone
            PBK Level 7 Level 7 (3,795 points)

            Seems like you have two choices:

            1) switch to ObjectiveC, it's not deprecated there:

            /*! This creates the record in the default zone. */
            - (instancetype)initWithRecordType:(CKRecordType)recordType;
            - (instancetype)initWithRecordType:(CKRecordType)recordType recordID:(CKRecordID *)recordID;
            - (instancetype)initWithRecordType:(CKRecordType)recordType zoneID:(CKRecordZoneID *)zoneID;
            @property (nonatomic, readonly, copy) CKRecordType recordType;
            @property (nonatomic, readonly, copy) CKRecordID *recordID;


            2) ignore the documents and warning in Swift

            • Re: New CKRecord in custom zone
              jmaraujo Level 1 Level 1 (0 points)



              I know this is a bit old now but I had the same problem and I've found the solution by trying to implement the deprecated initializer and following the swift sugestion under the warning:


              So the solution is:


              let recordID = CKRecord.ID(zoneID: myCustomZoneID)
              let newRecord = CKRecord(recordType: "Item", recordID: recordID)


              My previous mistake was to think that was not possible to use the CKRecord.ID initializer without specifing the "recordName:" like it suggest in the autocomplete.


              Hope this helps other people in future.

              • Re: New CKRecord in custom zone
                digitalglen Level 1 Level 1 (20 points)

                CKModifyRecordsOperation can take multiple records, and without recordIDs they wouldn't be able to be individually identified.


                Any reason why you're reluctant to create your own recordID?


                    let recordID = CKRecordID(recordName: UUID().uuidString, zoneID: myZoneID)

                • Re: New CKRecord in custom zone
                  fsdolphin Level 1 Level 1 (0 points)

                  Here is the code to save a record in a custom zone.


                  func addRecordToCustomZone() {       
                      let ckRecordZoneID = CKRecordZone(zoneName: "CustomZoneName")
                      let ckRecordID = CKRecord.ID(zoneID: ckRecordZoneID.zoneID)
                      let aRecord = CKRecord(recordType: "Cars", recordID: ckRecordID)// Cars is the data Type
                      aRecord["carMake"] = "Porche" // carMake is a field in the Cars Type.
                      aRecord["carModel"] = "Carrera" // carModel is a field in the Cars Type.
                      privateDatabase.save(aRecord, completionHandler: { (record, error) -> Void in
                          if let error = error {
                          else {
                              print("record saved successfully")


                  Please note that you also have the option of adding your own record name if you like by specifying it in the `CKRecord.ID` as follow.


                  let ckRecordID = CKRecord.ID(recordName: "SomeUniqueName", zoneID: ckRecordZoneID.zoneID)