An operation used to execute queries against a database.


class 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 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 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

init(query: CKQuery)

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

init(cursor: CKQueryOperation.Cursor)

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

Configuring the Operation Object

var query: CKQuery?

The query to use for the search.

var cursor: CKQueryOperation.Cursor?

The data cursor to use for continuing the search.

var zoneID: CKRecordZone.ID?

The ID of the zone containing the records to search.

var resultsLimit: Int

The maximum number of records to return at one time.

Processing the Operation Results

var recordFetchedBlock: ((CKRecord) -> Void)?

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


class let maximumResults: Int

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


class CKQueryOperation.Cursor

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


Inherits From

Conforms To

See Also


class CKQuery

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

class CKQueryOperation.Cursor

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

class CKLocationSortDescriptor

An object used to sort records containing location data.