Reputation: 771
I am trying to implement syncing with CloudKit. To fetch changes from the server i am using CKFetchRecordZoneChangesOperation. However recordZoneChangeTokensUpdatedBlock not called at all. Here is the code:
let options = CKFetchRecordZoneChangesOptions()
options.previousServerChangeToken = changesToken
let operation = CKFetchRecordZoneChangesOperation(recordZoneIDs: [paletteZoneId], optionsByRecordZoneID: [paletteZoneId:options])
operation.fetchAllChanges = true
operation.recordChangedBlock = {(record) in
...
}
operation.recordWithIDWasDeletedBlock = { (recordId,str) in
...
}
operation.recordZoneChangeTokensUpdatedBlock = { recordZoneId, token, data in
print("new token")
self.changesToken = token
}
operation.fetchRecordZoneChangesCompletionBlock = { error in
...
}
privateDB.add(operation)
So as the result operation is not working properly. Other blocks are called as expected.
Documentation says that token block should be called per zone, but it is not called at all.
I would really appreciate any help.
Many thanks.
Upvotes: 3
Views: 1104
Reputation: 2107
From the CKFetchRecordZoneChangesOperation header file about recordZoneChangeTokensUpdatedBlock:
Clients are responsible for saving this per-recordZone
serverChangeToken
and passing it in to the next call toCKFetchRecordZoneChangesOperation
.Note that a fetch can fail partway. If that happens, an updated change token may be returned in this block so that already fetched records don't need to be re-downloaded on a subsequent operation.
recordZoneChangeTokensUpdatedBlock
will not be called after the last batch of changes in a zone; therecordZoneFetchCompletionBlock
block will be called insteadThe
clientChangeTokenData
from the most recentCKModifyRecordsOperation
issued on this zone is also returned, or nil if none was provided.If the server returns a
CKErrorChangeTokenExpired
error, theserverChangeToken
used for this record zone when initting this operation was too old and the client should toss its local cache and re-fetch the changes in this record zone starting with a nilserverChangeToken
.
recordZoneChangeTokensUpdatedBlock
will not be called iffetchAllChanges
is NO.
Get function definition to see it. (Left click and select Jump to definition on recordZoneChangeTokensUpdatedBlock)
So for the developer, it means, that during the huge request (1000 of records for example) if some error occurs, a new request can be started from the point it was broken and only the rest of items can be downloaded. Practically, today, this callback is called for huge request after every 200 items were received, providing new change token. So the items that were received before an error occurred can be processed somehow and update can be triggered later on from that point.
Finally, the answer is: that block is called for a huge request. You can try it by creating a few hundreds of items and fetch them then.
Upvotes: 3
Reputation: 226
the server change token is returned in a different block - you need to use recordZoneFetchCompletionBlock.
Upvotes: 4