An operation used to execute queries against a database.
SDKs
- iOS 8.0+
- macOS 10.10+
- tvOS 9.0+
- watchOS 3.0+
Framework
- Cloud
Kit
Declaration
class CKQueryOperation : CKDatabase Operation
Overview
A CKQueryOperation object is a concrete operation that you can use to execute queries against a database. A query operation takes the query parameters you provide and applies those parameters to the specified database and zone, delivering any matching records asynchronously to the blocks that you provide.
To perform a new search:
Initialize a
CKQuery
object with aOperation CKQuery
object containing the search criteria and sorting information for the records you want.Assign a block to the
query
property so that you can process the results and execute the operation.Completion Block 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
CKQuery
object when you are ready to process the next batch of results.Operation Optionally configure the return results by specifying values for the
results
andLimit desired
properties.Keys Pass the query operation object to the
add(_:)
method of the target database to execute the operation against that database.
Queries are restricted to the records in a single zone. For new queries, you specify the zone when you initialize the query operation object. For cursor-based queries, the cursor contains the zone information. To search for records in multiple zones, you must create a separate CKQuery
object for each zone you want to search, although you can initialize each of them with the same CKQuery
object.
If you assign a completion block to the completion
property of the operation object, the completion block is called after the operation executes and returns its results to you. You can use a completion block to perform housekeeping chores related to the operation, but do not use it to process the results of the operation itself. Any completion block you specify should be prepared to handle the failure of the operation to complete its task, whether due to an error or an explicit cancellation.