Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CloudKit: Error saving record with CKModifyRecordsOperation

Tags:

ios

cloudkit

<CKError 0x14d8cb70: "Partial Failure" (2/1011); "Failed to modify some records"; partial errors: {
    B5DEF0B5-F064-4B27-9C89-BE75C9134297:(_defaultZone:__defaultOwner__) = <CKError 0x14d83b70: "Server Record Changed" (14/2037); "Error saving record <CKRecordID: 0x15748cd0; B5DEF0B5-F064-4B27-9C89-BE75C9134297:(_defaultZone:__defaultOwner__)> to server: Protection data didn't match">
}>

I get this error, when try to save CKRecords up to CloudKit. Any idea?

Do I have to fetch first records like in convenience API?

I am using CKModifyRecordsOperation method to update more record.

like image 541
János Avatar asked Mar 17 '23 10:03

János


1 Answers

When you are planning to do an update, you first need to read the record, make the changes and then write the record. You got a notification that the record was changed in the time between reading and writing the record.

If the version of a record on the server is newer than the version you tried to save, the server returns a CKErrorServerRecordChanged error. The userInfo dictionary of the error object contains the different versions of the conflicting records. Use these keys to retrieve the records and to perform whatever resolution logic is needed to resolve the conflict.

As discussed below in your case the problem was that you persisted the object to a database and recreated the CKRecord to do the modification. In that case you need to persist the system fields using CKRecord encodeSystemFieldsWithCoder. And recreate the CKRecord by initializing it with a NSCoder. You could use the NSKeyedArchiver and NSKeyedUnarchiver for saving a CKRecord and recreating it. If you need a sample, see the fromCKRecord and toCKRecord methods at the bottom of https://github.com/evermeer/EVCloudKitDao/blob/master/AppMessage/AppMessage/CloudKit/EVCloudKitDao.swift

like image 130
Edwin Vermeer Avatar answered Apr 25 '23 00:04

Edwin Vermeer