Contents

CKQueryOperation

An operation for executing queries in a database.

Declaration

class CKQueryOperation

Overview

A CKQueryOperation object is a concrete operation that you can use to execute queries. A query operation applies query parameters to the specified database and record zone, delivering any matching records asynchronously to the handlers that you provide.

To perform a new search:

  1. Initialize a CKQueryOperation object with a CKQuery object that contains the search criteria and sorting information for the records you want.

  2. Assign a handler 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. Use the cursor to initialize and execute a separate CKQueryOperation instance when you’re ready to process the next batch of results. 3. Optionally, configure the 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.

CloudKit restricts queries to the records in a single record 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 handler to the operation’s completionBlock property, the operation calls it after it executes and returns any results. Use a handler to perform housekeeping tasks for the operation, but don’t use it to process the results of the operation. The handler you provide should manage any failures, whether due to an error or an explicit cancellation.

Topics

Creating a Query Operation

Configuring the Query Operation

Processing the Query Results

Instance Properties

See Also

Queries