Limit the amount of results returned in CloudKit

前端 未结 1 1918
终归单人心
终归单人心 2021-01-02 11:07

Is there any way to limit the amount of results that are returned in a CKQuery?

In SQL, it is possible to run a query like SELECT * F

相关标签:
1条回答
  • 2021-01-02 11:28

    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)
    
    0 讨论(0)
提交回复
热议问题