As described in https://developer.apple.com/reference/cloudkit/ckserverchangetoken, the CloudKit servers return a change token as part of the CKFetchRecordZoneChangesOperation callback response. For what set of subsequent record fetches should I include the given change token in my fetch calls?
I, too, found the scope of CKServerChangeToken
a little unclear. However, after reviewing the documentation, both CKFetchDatabaseChangesOperation
and CKFetchRecordZoneChangesOperation
provide and manage their own server change tokens.
This is particularly useful if you decide to follow the CloudKit workflow Dave Browning outlines in his 2017 WWDC talk when fetching changes (around the 8 minute mark).
The recommended approach is to:
1) Fetch changes for a database using CKFetchDatabaseChangesOperation
. Upon receiving the updated token via changeTokenUpdatedBlock
, persist this locally. This token is 'scoped' to either the private or shared CKDatabase
the operation was added to. The public database doesn't offer change tokens.
2) If you receive zone IDs via the recordZoneWithIDChangedBlock
in the previous operation, this indicates there are zones which have changes you can fetch with CKFetchRecordZoneChangesOperation
. This operation takes in it's own unique server change token via it's rather cumbersome initializer parameter: CKFetchRecordZoneChangesOperation.ZoneConfiguration
. This is 'scoped' to this particular CKRecordZone
. So, again, when receiving an updated token via recordZoneChangeTokensUpdatedBlock
, it needs persisting locally (perhaps with a key which relates to it's CKRecordZone.ID
).
The benefit here is that it probably minimises the number of network calls. Fetching database changes first prevents making calls for each record zone if the database doesn't report any changed zone ids.
Here's a code sample from the CloudKit team which runs through this workflow. Admittedly a few of the APIs have since changed and the comments don't explicitly make it clear the 'scope' of the server change tokens.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With