Class

CKQueryOperation

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.

Overview

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.

Symbols

Initializing the Operation Object

init(query: CKQuery)

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

init(cursor: CKQueryCursor)

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: CKQueryCursor?

The data cursor to use for continuing the search.

var zoneID: CKRecordZoneID?

The ID of the zone containing the records to search.

var resultsLimit: Int

The maximum number of records to return at one time.

var desiredKeys: [String]?

The fields to retrieve for the requested records.

Processing the Operation Results

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

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

var queryCompletionBlock: ((CKQueryCursor?, Error?) -> Void)?

The block to execute with the search results.

Constants

Maximum Query Results Constants

Constant indicating the number of results to return for a query.

Initializers

Relationships

Inherits From