An operation used to execute queries against a database.
- iOS 8.0+
- macOS 10.10+
- Mac Catalyst 13.0+
- tvOS 9.0+
- watchOS 3.0+
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:
CKQueryobject with a
CKQueryobject containing the search criteria and sorting information for the records you want.
Assign a block to the
queryproperty 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
CKQueryobject when you are ready to process the next batch of results.
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
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.