Search code examples
iosobjective-cicloudcloudkit

CloudKit CKModifyRecordsOperation gives me a "Protection data didn't match"


I'm trying to upload some record changes with CloudKit. I'm truing to use a CKModifyRecordsOperation to do a batch upload of records that have changed on the device. The records are all in a custom zone

For some reason the operation keeps coming back with an error telling me ""Protection data didn't match"

Here's the code:

- (void)updloadLocalChangesWithCompletionBlock:(void (^)(NSError *error))completionBlock
{
   // Initialize the data
   NSArray *localChanges = self.localChanges;
   NSArray *localDeletions = self.localDeletions;

   // Initialize the database and modify records operation
   CKDatabase *database = [CKContainer defaultContainer].privateCloudDatabase;
   CKModifyRecordsOperation *modifyRecordsOperation = [[CKModifyRecordsOperation alloc] initWithRecordsToSave:localChanges recordIDsToDelete:localDeletions];
   modifyRecordsOperation.savePolicy = CKRecordSaveAllKeys;

   NSLog(@"CLOUDKIT Changes Uploading: %d", localChanges.count);

   // Add the completion block
   modifyRecordsOperation.modifyRecordsCompletionBlock = ^(NSArray *savedRecords, NSArray *deletedRecordIDs, NSError *error) {
       if (error) {
           NSLog(@"[%@] Error pushing local data: %@", self.class, error);
       }

       [self.localChanges removeObjectsInArray:savedRecords];
       [self.localDeletions removeObjectsInArray:deletedRecordIDs];

       completionBlock(error);
   };

   // Start the operation
   [database addOperation:modifyRecordsOperation];
}

Here is the error it's giving me:

[CloudKitSyncManager] Error Uploading Changes: <CKError 0x156654a0: "Partial Failure" (2/1011); "Failed to modify some records"; partial errors: {
    default-00001:(ZoneName:UserRecordID) = <CKError 0x1550b5a0: "Server Record Changed" (14/2037); "Error saving record <CKRecordID: 0x18080430; default-00001:(ZoneName:UserRecordID)> to server: Protection data didn't match">
}>

Solution

  • What exactly is in your self.localChanges array? CKRecord I assume, but are the modified records you're uploading based on the same CKRecord objects you downloaded from CloudKit?

    I had a similar (albeit not exact) error message when I tried uploading a freshly allocated CKRecord with my local changes and expected it to overwrite the server's copy. I fixed it by downloading the remote copy of my item, applying updates to the CKRecord instance given to me by CloudKit, and then uploading it.