Search code examples
swift3icloudcloudkit

Cloud Kit how to re fetch records when error


Cloud WWDC says When you get a CKError.changeTokenExpired re-fetch changes by setting the previous server change token to nil. I tried something like this:

let operation = CKFetchRecordZoneChangesOperation()
operation.qualityOfService = .userInitiated
operation.recordZoneIDs = appDelegate.changedZoneIDs

let fetchOptions = CKFetchRecordZoneChangesOptions()
fetchOptions.previousServerChangeToken = nil
operation.optionsByRecordZoneID = [ recordZoneID : fetchOptions]

But the optionsByRecordZoneID has been deprecated. So how can I pass the previous server token to the server and continue fetching past the error handling?


Solution

  • You should use the instance property configurationsByRecordZoneID on your CKFetchRecordZoneChangesOperation().

    How about:

    let operation = CKFetchRecordZoneChangesOperation()
    operation.qualityOfService = .userInitiated
    operation.recordZoneIDs = appDelegate.changedZoneIDs
    
    let fetchOptions = CKFetchRecordZoneChangesOperation.ZoneConfiguration()
    fetchOptions.previousServerChangeToken = nil
    
    var zoneConfiguration: [CKRecordZone.ID : CKFetchRecordZoneChangesOperation.ZoneConfiguration] = [:]
    for zoneID in operation.recordZoneIDs {
        zoneConfiguration[zoneID] = fetchOptions
    }
    operation.configurationsByRecordZoneID = zoneConfiguration