我正在尝试使用CloudKit上传一些记录更改。我正试图使用​​CKModifyRecordsOperation来批量上传设备上已更改的记录。记录都在自定义区域中

由于某种原因,该操作不断返回错误,提示我“保护数据不匹配”

这是代码:

- (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];
}

这是它给我的错误:
[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">
}>

最佳答案

您的self.localChanges数组到底是什么?我假设使用CKRecord,但是您要上传的修改记录是否基于您从CloudKit下载的相同CKRecord对象?

当我尝试上载具有本地更改的新分配的CKRecord并希望它会覆盖服务器的副本时,出现类似的错误消息(尽管不完全)。我通过下载项目的远程副本,将更新应用于CloudKit给我的CKRecord实例并上载来修复了它。

10-08 12:30