Cloud Kit如何在出错时重新获取记录

Cloud Kit how to re fetch records when error

Cloud WWDC 说当您获得 CKError.changeTokenExpired 时,通过将先前的服务器更改令牌设置为零来重新获取更改。我试过这样的事情:

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

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

但是 optionsByRecordZoneID 已被弃用。那么如何将之前的服务器令牌传递给服务器并继续获取错误处理?

您应该在 CKFetchRecordZoneChangesOperation() 上使用实例 属性 configurationsByRecordZoneID。

怎么样:

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