简体   繁体   English

CloudKit-每次运行相同查询时CKQueryOperation结果不同

[英]CloudKit - CKQueryOperation results different each time the same query is ran

This is the case - I'm using a simple UITableView that renders records from the CloudKit publicDB. 就是这种情况-我正在使用一个简单的UITableView来呈现CloudKit publicDB中的记录。 When I run the app, the query operation returns for example returns 2 results (that's all it has currently). 当我运行应用程序时,查询操作返回例如返回2个结果(这是当前的全部结果)。

My table view has a refresh control and when I pull to refresh I got zero results, if I continue to do reloads, eventually a result might come out but now always. 我的表视图具有刷新控件,当我进行刷新操作时,我得到的结果为零,如果我继续进行重新加载,最终可能会出现结果,但现在总是如此。

The same thing happens with more results as well, I used to have CKLocation type which I queried and the response was always different without any common sense 同样的事情也会产生更多的结果,我曾经查询过CKLocation类型,并且在没有任何常识的情况下响应总是不同的

Some example code (the predicate in this case is TRUEPREDICATE - nothing fancy): 一些示例代码(本例中的谓词为TRUEPREDICATE-没什么花哨的):

    let sort = NSSortDescriptor(key: "creationDate", ascending: false)        
    let query = CKQuery(recordType: "Tests", predicate: DiscoveryMode.getPredicate())
    query.sortDescriptors = [sort]

    var operation = CKQueryOperation(query: query)
    if lastCursor != nil {
        operation = CKQueryOperation(cursor: lastCursor)
    }

    operation.resultsLimit = 15
    operation.recordFetchedBlock = recordFetchBlock
    operation.queryCompletionBlock = { [weak self] (cursor:CKQueryCursor!, error:NSError!) in

        if cursor != nil {
             self!.lastCursor = cursor
        }

        dispatch_async(dispatch_get_main_queue(), { () -> Void in

            Misc.hideLoadingInView(view: self!.view)

            self!.tableView.reloadData()
            self!.refreshControl.endRefreshing()

            if error != nil {
                Misc.showErrorInView(view: self!.view, message: error.localizedDescription)
            }
        })
    }

    CloudKit.sharedInstance.publicDB.addOperation(operation)

All the recordFetchBlock does is to add objects to a mutable array that the table view uses as dataSource. recordFetchBlock所做的全部工作就是将对象添加到表视图用作数据源的可变数组中。

I'm new to CloudKit and I'm puzzled is this by design (not returning all the results but some random) or I'm doing something wrong? 我是CloudKit的新手,我很困惑,这是设计使然(不返回所有结果,而是返回一些随机结果)还是我做错了什么?

I see that you are using a cursor. 我看到您正在使用游标。 because of that the 2nd call will start at the point where the first call ended. 因此,第二个通话将在第一个通话结束的地方开始。 You have a resultsLimit of 15. When using a cursor, you will only receive records the 2nd time you execute the query if there were more than 15 records. 您的resultLimit为15。使用游标时,如果有超过15条记录,则您只会在第二次执行查询时收到记录。 To test if this is the issue just comment out the line where you set the cursor: operation = CKQueryOperation(cursor: lastCursor) 要测试这是否是问题,只需注释掉设置光标的行:operation = CKQueryOperation(cursor:lastCursor)

I found the issue, I was trying to do (in the NSPredicate) a radius with a value I read somewhere is in kilometers. 我发现了问题,我正在尝试(在NSPredicate中)以某处读取的值为单位的半径为千米。 Therefore I was trying to query records within 500 meters instead of 500 kilometers and the GPX file I'm using in the simulator has multiple records with a larger distance. 因此,我试图查询500米而不是500公里内的记录,并且我在模拟器中使用的GPX文件包含多个具有较大距离的记录。 Since it simulates movement, that was the reason not to get consistent results. 由于它模拟运动,因此没有得到一致的结果。

Now, when I'm using a proper value for the radius all seems to be just fine! 现在,当我为半径使用适当的值时,一切似乎都很好!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM