swiftcloudkitckrecord

CloudKit - CKRecord.ID acceptable recordName?


I've written a method to save records in a custom zone and it seems to be working as expected. One thing I'm not sure about, however, is the CKRecord.ID recordName. Right now I just use a UUID string. Is there a preferred way to assign the recordName? Current CloudKit examples are pretty scarce and it seems like a decent portion of the CK documentation is outdated. Thanks.

func saveToCloud(record: String, recordType: String, recordTypeField: String, reference: CKRecord?, referenceType: String?) {

    let zoneID = CKRecordZone.ID(zoneName: Zone.test, ownerName: CKRecordZone.ID.default.ownerName)
    let recordID = CKRecord.ID(recordName: UUID().uuidString, zoneID: zoneID)
    let newRecord = CKRecord(recordType: recordType, recordID: recordID)

    if let reference = reference, let referenceType = referenceType {
        let newReference = CKRecord.Reference(record: reference, action: .none)
        newRecord[referenceType] = newReference
    }
    newRecord[recordTypeField] = record
    database.save(newRecord) { (_,error) in
        if let err = error as? CKError {
            print("ERROR =" , err.userInfo )
        }
    }
}

Solution

  • From my point of view its very clear in the docs of CKRecord.ID:

    A record ID object consists of a name string and a zone ID. The name string is an ASCII string not exceeding 255 characters in length. For automatically created records, the ID name string is based on a UUID and is therefore guaranteed to be unique. When creating your own record ID objects, you are free to use names that have more meaning to your app or to the user, as long as each name is unique within the specified zone. For example, you might use a document name for the name string.