Can't retrieve CKRecords to resolve conflicts on CKErrorCodeServerRecordChanged in CKModifyRecordsOperation

879 Views Asked by At

I ran into some problem when trying to handle errors when doing batch operations on records in CloudKit.

I am successfully extracting the dictionary containing the partial errors, which I can iterate over. However, I am not able to get the records needed to resolve the conflict for CKErrorCodeServerRecordChanged. According to the docs I should be able to get 3 records out of the dictionary:

  • CKRecordChangedErrorServerRecordKey
  • CKRecordChangedErrorAncestorRecordKey
  • CKRecordChangedErrorClientRecordKey

Thank you for any hints on what I am doing wrong here.

func pushRecordChangesForZoneID(recordZoneID: CKRecordZoneID) {
// ...
modifyRecordsOperation.modifyRecordsCompletionBlock = { (savedRecords, deletedRecordIDs, error) -> Void in
    if (error != nil) {
        if error.code == CKErrorCode.PartialFailure.rawValue {
            if let errorDict = error.userInfo?[CKPartialErrorsByItemIDKey] as? [CKRecordID : NSError] {
                for (recordID, partialError) in errorDict {
                    if partialError.code == CKErrorCode.ServerRecordChanged.rawValue {
                        if let userInfo = partialError.userInfo {
                            let serverRecord = userInfo[CKRecordChangedErrorServerRecordKey] as? CKRecord
                            // serverRecord will always be nil
                        }
                    }
                }
            }
        }
    }
}

}

Additional information: When I print the description of the userInfo dict of the partial error (partialError.userInfo) it doesn’t look like it contains the other CKRecords:

[NSDebugDescription: CKInternalErrorDomain: 2037, NSLocalizedDescription: Error saving record <CKRecordID: 0x7fb41bf7e640; DA39FE08-AB0B-4F07-A42E-F5732B114706:(userData:__defaultOwner__)> to server: Protection data didn't match, NSUnderlyingError: <CKError 0x7fd89a92d370: "Unknown Error" (2037)>]

The description of the source error's dictionary (errorDict) looks like this (and I can successfully get the dictionary containing the recordIDs and partial errors out via CKPartialErrorsByItemIDKey):

[<CKRecordID: 0x7fb5bb88afa0; C1575083-F992-448A-8D77-D62C4A42D696:(userData:__defaultOwner__)>: <CKError 0x7fb5b961c6a0: "Batch Request Failed" (22/2024); server message = "Atomic failure"; uuid = 1E4C0FD5-EC10-4071-B277-102A9F1B0E5E; container ID = "iCloud.net.neverthesamecolor.atsumeru">, <CKRecordID: 0x7fb5bb848ad0; DA39FE08-AB0B-4F07-A42E-F5732B114706:(userData:__defaultOwner__)>: <CKError 0x7fb5b9653060: "Server Record Changed" (14/2037); "Error saving record <CKRecordID: 0x7fb41bd9ca50; DA39FE08-AB0B-4F07-A42E-F5732B114706:(userData:__defaultOwner__)> to server: Protection data didn't match">]
1

There are 1 best solutions below

1
On

The docs say it's a custom zone feature only.

Edit 21st Jan 2016: It's working for me right now even in the default zone and public database. This is a big change from before.