Search code examples
ioscloudkit

Cloudkit CKRecordZoneNotification how to know whether Add or modify happened


I am trying to implement Cloudkit sync with local cache (CoreData).

So far I managed to get the recordZone defined and get the relevant notifications. In the next step I check with CKFetchRecordChangesOperation what happened.

  • recordChangedBlock (i.e. according to Apple: ...for each record in the zone that changed since the previous fetch request....) I do get the relevant record, but how do I know, whether this record was added or modified (without checking against my local cache)?

  • recordWithIDWasDeletedBlock I get the recordId, but how do I know which record it is in my local cache ? I could think of storing the recordId in the local cache in order to have a reference for such cases, but I can't believe that this is what I'm supposed to do...

Any suggestion is more than appreciated


Solution

  • There is no info in the recordChangedBlock that tells you whether it was added or changed. Keep in mind, even if it did, you still have to check whether the record exists or not in the local store. A record can be added into CloudKit and then changed several times all while your app isn't running. When your app finally runs, it will get only the last change notification. But the record doesn't exist in your local cache yet. So you must always see if you have the record locally or not and add/update accordingly.

    With deletion, all you get is the CloudKit record id. Nothing else. What I do is ensure the CloudKit record id is based on the local key. This way I can easily find and remove the local record when the data is removed from Cloudkit. It also means that the local copy of the CloudKit data on all of the user's devices ends up with the same keys.