Search code examples
ioscloudkit

Incremental Sync using CloudKit -> How to get notified about deleted records


In my app, I'd like to sync data between my devices. I can get changes by asking for entities with an modificationDate greater than something... That way I get all new and modifier entities since a given date. So far so good.

Now users will be able to delete entities as well. What is the best way using CloudKit to get those recordIDs? Is there any way? Is there a way to ask for record ID's that got deleted since a given date?

Or will I only be able to archive that using a soft delete?!?


Solution

  • If you are storing a local cache of all of the items, use CKFetchRecordChangesOperation.

    This functionality is literally built for "when you maintain a local cache of your record data and need to synchronize that cache periodically with the server", and provides changed records and deleted record IDs.

    Note, however, that it only works in non-default zones, which means it won't work:

    • in the public database (which does not support custom zones)
    • in the private database's default zone

    You'll need to create a custom zone in the private database using CKModifyRecordZonesOperation.

    If you're storing the user's private data, this is the way to go.