What is clientChangeTokenData in CKModifyRecordsOperation?

955 views Asked by At

I am working on CloudKit sync in my app ("Tiny data, all devices" model, with a custom zone in the private database).

CKModifyRecordsOperation contains clientChangeTokenData property of NSData type which is described in the docs as follows:

When you modify records from a fetch operation, specify a client-generated data token using this property to indicate which version of the record you last modified. Compare the data token you supplied to the data token in the next record fetch to confirm the server has successfully received the device’s last modify request.

I don't get why I should bother given that with each request, I get a completion block which tells me whether the server has successfully received my request. Why do I need to manually compare this client token?

Is specifying clientChangeTokenData required to handle my use case correctly? I track local data changes and push everything in the queue on each data change. Remote changes are tracked via zone subscription.

If it is required, how do I generate this token correctly given that I have all kinds of record changes in my CKModifyRecordsOperation (my API usage aims for batch operations). What is the general workflow here?

Thank you.

2

There are 2 answers

3
Edwin Vermeer On

You would only have a reason to check the token if you had local changes that you want to write to CloudKit and you want to make sure that your changes are based on the latest version of the data in CloudKit.

You could also just ignore the token and save the data anyway. If the data has changed in the mean time, you will get a CloudKit error and you could handle it then.

0
malhal On

It's unclear from the docs so I'd guess the clientChangeTokenData is useful in the case of sending up a large modify records operation, e.g. deleting 100 records. Then say your app sends a fetch request in another operation with a query (or fetch changes) result set that would be affected by the modifications which either:

  1. is started and is running concurrently to the existing modify operation which hasn't finished yet.
  2. is started before the server has finished processing the results of the previous modifications (the docs tend to allude to a processing delay).

If the fetch completion contains a different clientChangeTokenData to the one sent with the modify then you know it hasn't received (or finished processing?) the changes yet. In this situation you could either error, with an alert to say the server needs more time, or automatically retry the fetch after some time.

By the way in my tests, this token is per-device.