CKDatabase
An object that represents a collection of record zones and subscriptions.
Declaration
class CKDatabaseMentioned 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:
Create an instance of CKOperation.Configuration and set its qualityOfService property to the preferred value.
Call the databaseʼs configuredWith(configuration:group:body:) method and provide the configuration and a trailing closure.
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
records(for:desiredKeys:)fetch(withRecordIDs:desiredKeys:completionHandler:)fetch(withRecordID:completionHandler:)
Querying Records
records(matching:inZoneWith:desiredKeys:resultsLimit:)records(continuingMatchFrom:desiredKeys:resultsLimit:)fetch(withQuery:inZoneWith:desiredKeys:resultsLimit:completionHandler:)fetch(withCursor:desiredKeys:resultsLimit:completionHandler:)perform(_:inZoneWith:completionHandler:)records(matching:inZoneWith:)
Modifying Records
modifyRecords(saving:deleting:savePolicy:atomically:)modifyRecords(saving:deleting:savePolicy:atomically:completionHandler:)CKModifyRecordsOperation.RecordSavePolicysave(_:completionHandler:)delete(withRecordID:completionHandler:)
Fetching Record Zones
recordZones(for:)fetch(withRecordZoneIDs:completionHandler:)fetchAllRecordZones(completionHandler:)fetch(withRecordZoneID:completionHandler:)
Modifying Record Zones
modifyRecordZones(saving:deleting:)modifyRecordZones(saving:deleting:completionHandler:)save(_:completionHandler:)delete(withRecordZoneID:completionHandler:)
Fetching Subscriptions
subscriptions(for:)fetch(withSubscriptionIDs:completionHandler:)subscription(for:)fetch(withSubscriptionID:completionHandler:)fetchAllSubscriptions(completionHandler:)
Modifying Subscriptions
modifySubscriptions(saving:deleting:)modifySubscriptions(saving:deleting:completionHandler:)save(_:completionHandler:)deleteSubscription(withID:)delete(withSubscriptionID:completionHandler:)
Fetching Changes
databaseChanges(since:resultsLimit:)fetchDatabaseChanges(since:resultsLimit:completionHandler:)CKDatabase.DatabaseChangerecordZoneChanges(inZoneWith:since:desiredKeys:resultsLimit:)fetchRecordZoneChanges(inZoneWith:since:desiredKeys:resultsLimit:completionHandler:)CKDatabase.RecordZoneChange