我遇到了这样的问题:我使用CloudKit将某些内容保存到icloud,但是立即获取结果并不会返回插入的最新数据。
例子
let todoRecord = CKRecord(recordType: "Todos")
todoRecord.setValue(todo, forKey: "todotext")
publicDB.saveRecord(todoRecord, completionHandler: { (record, error) -> Void in
NSLog("Saved in cloudkit")
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "Todos",
predicate: predicate)
self.publicDB.performQuery(query, inZoneWithID: nil) {
results, error in
if error != nil {
dispatch_async(dispatch_get_main_queue()) {
self.delegate?.errorUpdating(error)
return
}
} else {
NSLog("###### fetch after save : \(results.count)")
dispatch_async(dispatch_get_main_queue()) {
self.delegate?.modelUpdated()
return
}
}
}
结果 :
Before saving in cloud kit : 3
CloudKit[22799:882643] Saved in cloudkit
CloudKit[22799:882643] ###### Count after save : 3
我在这里想念什么吗?
最佳答案
在将记录保存在CloudKit中与使用该记录中的值更新索引之间存在一个延迟。CKModifyRecordsOperation
成功完成后,您可以立即通过其记录标识符获取该记录。
但是,将记录添加到服务器上的搜索索引时会有一个延迟,查询将不会立即找到该记录。
如果您使用CKQuery
来备份 View ,则需要保留在本地修改过的记录的边表,并将这些记录缝合到 View 中,直到查询开始返回该记录为止。
关于ios - CloudKit不返回最新数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26375113/