合理使用缓存:尝试在合适的时候使用CloudKit的缓存功能,缓存返回的数据以减少对服务器的请求次数。缓存策略设置如下:let container = CKContainer.default()
let publicDatabase = container.publicCloudDatabase
let cachePolicy = CKFetchRecordZoneChangesOperation.ZoneConfiguration.CachePolicy.afterMerge
let zoneConfig = CKFetchRecordZoneChangesOperation.ZoneConfiguration()
zoneConfig.cachePolicy = cachePolicy
zoneConfig.recordZoneID = CKRecordZone.ID(zoneName: "exampleZone", ownerName: "exampleOwner")
let options = CKFetchRecordZoneChangesOperation.Options()
options.previousServerChangeToken = nil
options.resultsLimit = 10
let operation = CKFetchRecordZoneChangesOperation()
operation.configurationByRecordZoneID = [zoneConfig.zoneID: zoneConfig]
operation.fetchAllChanges = false
operation.recordChangedBlock = { (record) -> Void in
// handle changed record
}
operation.recordWithIDWasDeletedBlock = { (recordID, recordType) -> Void in
// handle deleted record
}
operation.recordZoneFetchCompletionBlock = { (zoneID, serverChangeToken, clientChangeTokenData, moreComing, error) -> Void in
// handle zone fetch completion
}
operation.fetchRecordZoneChangesCompletionBlock = { (error) -> Void in
// handle fetch completion
}
publicDatabase.add(operation)