Class

CKDatabase

A conduit for accessing and for performing operations on the public and private data of an app container.

Overview

An app container has a public database whose data is accessible to all users and a private database whose data is accessible only to the current user. A database object takes requests for data and applies them to the appropriate part of the container.

You do not create database objects yourself, nor should you subclass CKDatabase. Your app’s CKContainer objects provide the CKDatabase objects you use to access the associated data. Use database objects as-is to perform operations on data.

The public database is always available, regardless of whether the device has an an active iCloud account. When no iCloud account is available, your app may fetch records and perform queries on the public database, but it may not save changes. (Saving records to the public database requires an active iCloud account to identify the owner of those records.) Access to the private database always requires an active iCloud account on the device.

Interacting with Database Objects

You interact with database objects as follows:

  • Assign a database object to an operation object when you want to fetch, save, delete, and query the contents of the database. The database object tells the operation object where to get the data. (Calling the add(_:) method of the database assigns that database to the operation object implicitly.)

  • Use a database object’s convenience methods to fetch, save, delete, and query the contents of the database directly. The convenience methods act on only one item at a time.

Using Databases with Operation Objects

For operation objects that descend from the CKDatabaseOperation class, specify the database object to use when executing the operation.

  • If you execute operations on your own custom queue, specify the database explicitly by assigning the appropriate CKDatabase object to the operation object’s database property.

  • If you do not have your own queue, execute the operation object against a database implicitly by calling its add(_:) method.

Executing Tasks Against a Database Directly

The CKDatabase class offers convenience methods for accessing records, record zones, and subscriptions without an operation object. The convenience methods allow you to fetch, save, or delete a single item asynchronously and process the results on a background thread. There is also a convenience method to search for records in the database.

Symbols

Searching for Records

func perform(CKQuery, inZoneWith: CKRecordZoneID?, completionHandler: ([CKRecord]?, Error?) -> Void)

Searches the specified zone asynchronously for records that match the query parameters.

Executing Operations Against the Database

func add(CKDatabaseOperation)

Executes the specified operation asynchronously against the current database.

Accessing Records

func fetch(withRecordID: CKRecordID, completionHandler: (CKRecord?, Error?) -> Void)

Fetches one record asynchronously, with a low priority, from the current database.

func save(CKRecord, completionHandler: (CKRecord?, Error?) -> Void)

Saves one record asynchronously, with a low priority, to the current database, if the record has never been saved or if it is newer than the version on the server.

func delete(withRecordID: CKRecordID, completionHandler: (CKRecordID?, Error?) -> Void)

Deletes the specified record asynchronously, with a low priority, from the current database.

Accessing Record Zones

func fetchAllRecordZones(completionHandler: ([CKRecordZone]?, Error?) -> Void)

Fetches all record zones asynchronously, with a low priority, from the current database.

func fetch(withRecordZoneID: CKRecordZoneID, completionHandler: (CKRecordZone?, Error?) -> Void)

Fetches one record zone asynchronously, with a low priority, from the current database.

func save(CKRecordZone, completionHandler: (CKRecordZone?, Error?) -> Void)

Saves one record zone asynchronously, with a low priority, to the current database.

func delete(withRecordZoneID: CKRecordZoneID, completionHandler: (CKRecordZoneID?, Error?) -> Void)

Deletes one record zone (and its contents) asynchronously, with a low priority, from the current database.

Accessing Subscriptions

func fetchAllSubscriptions(completionHandler: ([CKSubscription]?, Error?) -> Void)

Fetches all subscription objects asynchronously, with a low priority, from the current database.

func fetch(withSubscriptionID: String, completionHandler: (CKSubscription?, Error?) -> Void)

Fetches one subscription object asynchronously, wiht a low priority, from the current database.

func save(CKSubscription, completionHandler: (CKSubscription?, Error?) -> Void)

Saves one subscription object asynchronously, with a low priority, to the current database.

func delete(withSubscriptionID: String, completionHandler: (String?, Error?) -> Void)

Deletes one subscription object asynchronously, with a low priority, from the current database.

Accessing the Scope of the Database

var databaseScope: CKDatabaseScope

The type of database (public, private, or shared).

Constants

CKDatabaseScope

The database scope (public, private, or shared).

Relationships

Inherits From

Conforms To