Contents

CKDatabase

An object that represents a collection of record zones and subscriptions.

Declaration

class CKDatabase

Mentioned in

Overview

A database takes requests and operations and applies them to the objects it contains, whether that’s record zones, records, or subscriptions. Each of your app’s users has access to the three separate databases:

  • A public database that’s accessible to all users of your app.

  • A private database that’s accessible only to the user of the current device.

  • A shared database that’s accessible only to the user of the current device, which contains records that other iCloud users share with them.

The public database is always available, even when the device doesn’t have an active iCloud account. In this scenario, your app can fetch specific records and perform searches, but it can’t create or modify records. CloudKit requires an iCloud account for writing to the public database so it can identify the authors of any changes. All access to the private and shared databases requires an iCloud account.

You don’t create instances of CKDatabase, nor do you subclass it. Instead, you access the required database using one of your app’s containers. For more information, see CKContainer.

By default, CloudKit executes the methods in this class with a low-priority quality of service (QoS). To use a higher-priority QoS, perform the following:

  1. Create an instance of CKOperation.Configuration and set its qualityOfService property to the preferred value.

  2. Call the databaseʼs configuredWith(configuration:group:body:) method and provide the configuration and a trailing closure.

  3. In the closure, use the provided database to execute the relevant methods at the preferred QoS.

func fetchRecords(
    with ids: [CKRecord.ID]
) async throws -> [CKRecord.ID: Result<CKRecord, any Error>] {

    // Get a reference to the user's private database.
    let database = CKContainer.default().privateCloudDatabase

    // Create a configuration with a higher-priority quality of service.
    let config = CKOperation.Configuration()
    config.qualityOfService = .userInitiated

    // Configure the database and execute the fetch.
    return try await database.configuredWith(configuration: config) { db in
        try await db.records(for: ids)
    }
}

Topics

Configuring Database Requests

Fetching Records

Querying Records

Modifying Records

Fetching Record Zones

Modifying Record Zones

Fetching Subscriptions

Modifying Subscriptions

Fetching Changes

Running Operations

Getting the Database Type

See Also

Core objects