I am having this issue where I save something to the icloud using CloudKit but immediately fetching the results doesn't return the latest data inserted.
我遇到了这个问题,我使用CloudKit向icloud保存了一些内容,但是立即获取结果并不会返回插入的最新数据。
Example
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
}
}
}
Result :
Before saving in cloud kit : 3
CloudKit[22799:882643] Saved in cloudkit
CloudKit[22799:882643] ###### Count after save : 3
Am I missing something here guys?
我在这里错过了什么吗?
1 个解决方案
#1
13
There is a delay between when a record is saved in CloudKit and when the indexes have been updated with values from that record.
在CloudKit中保存记录与使用该记录中的值更新索引之间存在延迟。
When a CKModifyRecordsOperation
completes successfully you are able to immediately fetch that record via its record identifier.
当CKModifyRecordsOperation成功完成时,您可以通过其记录标识符立即获取该记录。
However, there is a delay while the record is added to the search indexes on the server and queries won't find that record immediately.
但是,将记录添加到服务器上的搜索索引时会出现延迟,并且查询将不会立即找到该记录。
If you're using a CKQuery
to back a view you'll want to keep a side table of records that have been modified locally and stitch those into the view until the query starts returning that record.
如果您使用CKQuery来支持视图,则需要保留已在本地修改的记录的边表,并将这些记录组合到视图中,直到查询开始返回该记录。
#1
13
There is a delay between when a record is saved in CloudKit and when the indexes have been updated with values from that record.
在CloudKit中保存记录与使用该记录中的值更新索引之间存在延迟。
When a CKModifyRecordsOperation
completes successfully you are able to immediately fetch that record via its record identifier.
当CKModifyRecordsOperation成功完成时,您可以通过其记录标识符立即获取该记录。
However, there is a delay while the record is added to the search indexes on the server and queries won't find that record immediately.
但是,将记录添加到服务器上的搜索索引时会出现延迟,并且查询将不会立即找到该记录。
If you're using a CKQuery
to back a view you'll want to keep a side table of records that have been modified locally and stitch those into the view until the query starts returning that record.
如果您使用CKQuery来支持视图,则需要保留已在本地修改的记录的边表,并将这些记录组合到视图中,直到查询开始返回该记录。