CloudKit不返回最新数据 [英] CloudKit not returning the most recent data

查看:101
本文介绍了CloudKit不返回最新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这个问题,我使用CloudKit向icloud保存了一些内容,但是立即获取结果并不会返回插入的最新数据。

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.

示例

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中保存记录与使用该记录中的值更新索引之间存在延迟。

There is a delay between when a record is saved in CloudKit and when the indexes have been updated with values from that record.

CKModifyRecordsOperation 成功完成后,您可以通过其记录标识符立即获取该记录。

When a CKModifyRecordsOperation completes successfully you are able to immediately fetch that record via its record identifier.

然而,当记录添加到服务器上的搜索索引时会有延迟,查询将不会立即找到该记录。

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.

如果您使用 CKQuery 来备份视图,您将需要保留已在本地修改的记录的边表并将其拼接到视图中直到查询开始返回该记录。

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.

这篇关于CloudKit不返回最新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆