Limit the amount of results returned in CloudKit

寵の児 提交于 2019-11-30 14:04:10

You submit your CKQuery to a CKQueryOperation. The CKQueryOperation has concepts of cursor and of resultsLimit; they will allow you to bundle your query results. As described in the documentation:

To perform a new search:

1) Initialize a CKQueryOperation object with a CKQuery object containing the search criteria and sorting information for the records you want.

2) Assign a block to the queryCompletionBlock property so that you can process the results and execute the operation.

If the search yields many records, the operation object may deliver a portion of the total results to your blocks immediately, along with a cursor for obtaining the remaining records. If a cursor is provided, use it to initialize and execute a separate CKQueryOperation object when you are ready to process the next batch of results.

3) Optionally configure the return results by specifying values for the resultsLimit and desiredKeys properties.

4) Pass the query operation object to the addOperation: method of the target database to execute the operation against that database.

So it looks like:

var q   = CKQuery(/* ... */)
var qop = CKQueryOperation (query: q)

qop.resultsLimit = 5
qop.queryCompletionBlock = { (c:CKQueryCursor!, e:NSError!) -> Void in
  if nil != c {
    // there is more to do; create another op
    var newQop = CKQueryOperation (cursor: c!)
    newQop.resultsLimit = qop.resultsLimit
    newQop.queryCompletionBlock = qop.queryCompletionBlock

    // Hang on to it, if we must
    qop = newQop

    // submit
    ....addOperation(qop)
  }
}

....addOperation(qop)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!