简体   繁体   中英

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. When I run the app, the query operation returns for example returns 2 results (that's all it has currently).

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

Some example code (the predicate in this case is TRUEPREDICATE - nothing fancy):

    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.

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?

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. To test if this is the issue just comment out the line where you set the cursor: 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. 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. 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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