An operation used to execute queries against a database.


@interface CKQueryOperation : CKDatabaseOperation


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:

  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.

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 CKQueryOperation 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 completionBlock 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.


Initializing the Operation Object

- initWithQuery:

Initializes and returns an operation object configured to search for records in the specified zone.

- initWithCursor:

Initializes and returns an operation object that returns more results from a previous search.

Configuring the Operation Object


The query to use for the search.


The data cursor to use for continuing the search.


The ID of the zone containing the records to search.


The maximum number of records to return at one time.


The fields to retrieve for the requested records.

Processing the Operation Results


The block to execute for each record returned by the query.


The block to execute with the search results.



A placeholder value representing the maximum number of results to retrieve.


Inherits From

See Also



A query that describes the criteria to apply when searching for records in a database.


An object that marks the stopping point for a query and the starting point for retrieving the remaining results.


An object used to sort records containing location data.