<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.10.0 -",
    "tvOS: -",
    "visionOS: 1.0.0 -",
    "watchOS: 3.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CloudKit",
  "identifier" : "/documentation/CloudKit/CKDatabase",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "CloudKit"
    ],
    "preciseIdentifier" : "c:objc(cs)CKDatabase"
  },
  "title" : "CKDatabase"
}
-->

# CKDatabase

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

```
class CKDatabase
```

## 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`](/documentation/CloudKit/CKDatabase), nor do you subclass it. Instead, you access the required database using one of your app’s containers. For more information, see [`CKContainer`](/documentation/CloudKit/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`](/documentation/CloudKit/CKOperation/Configuration-swift.class) and set its [`qualityOfService`](/documentation/CloudKit/CKOperation/Configuration-swift.class/qualityOfService) property to the preferred value.
2. Call the databaseʼs [`configuredWith(configuration:group:body:)`](/documentation/CloudKit/CKDatabase/configuredWith(configuration:group:body:)-637p1) 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.

```swift
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

[`func configuredWith<R>(configuration: CKOperation.Configuration?, group: CKOperationGroup?, body: (CKDatabase) async throws -> R) async rethrows -> R`](/documentation/CloudKit/CKDatabase/configuredWith(configuration:group:body:)-637p1)

Applies a temporary configuration to the database within the scope of a closure that supports concurrency.

[`func configuredWith<R>(configuration: CKOperation.Configuration?, group: CKOperationGroup?, body: (CKDatabase) throws -> R) rethrows -> R`](/documentation/CloudKit/CKDatabase/configuredWith(configuration:group:body:)-12vrs)

Applies a temporary configuration to the database within the scope of a closure.

### Fetching Records

[`func records(for: [CKRecord.ID], desiredKeys: [CKRecord.FieldKey]?) async throws -> [CKRecord.ID : Result<CKRecord, any Error>]`](/documentation/CloudKit/CKDatabase/records(for:desiredKeys:))

Fetches the specified records and returns them to an awaiting caller.

[`func fetch(withRecordIDs: [CKRecord.ID], desiredKeys: [CKRecord.FieldKey]?, completionHandler: (Result<[CKRecord.ID : Result<CKRecord, any Error>], any Error>) -> Void)`](/documentation/CloudKit/CKDatabase/fetch(withRecordIDs:desiredKeys:completionHandler:))

Fetches the specified records and delivers them to a completion handler.

[`func fetch(withRecordID: CKRecord.ID, completionHandler: (CKRecord?, (any Error)?) -> Void)`](/documentation/CloudKit/CKDatabase/fetch(withRecordID:completionHandler:))

Fetches a specific record.

### Querying Records

[`func records(matching: CKQuery, inZoneWith: CKRecordZone.ID?, desiredKeys: [CKRecord.FieldKey]?, resultsLimit: Int) async throws -> (matchResults: [(CKRecord.ID, Result<CKRecord, any Error>)], queryCursor: CKQueryOperation.Cursor?)`](/documentation/CloudKit/CKDatabase/records(matching:inZoneWith:desiredKeys:resultsLimit:))

Searches for records that match a predicate and returns them to an awaiting caller.

[`func records(continuingMatchFrom: CKQueryOperation.Cursor, desiredKeys: [CKRecord.FieldKey]?, resultsLimit: Int) async throws -> (matchResults: [(CKRecord.ID, Result<CKRecord, any Error>)], queryCursor: CKQueryOperation.Cursor?)`](/documentation/CloudKit/CKDatabase/records(continuingMatchFrom:desiredKeys:resultsLimit:))

Retrieves the next batch of records from an existing search and returns them to an awaiting caller.

[`func fetch(withQuery: CKQuery, inZoneWith: CKRecordZone.ID?, desiredKeys: [CKRecord.FieldKey]?, resultsLimit: Int, completionHandler: (Result<(matchResults: [(CKRecord.ID, Result<CKRecord, any Error>)], queryCursor: CKQueryOperation.Cursor?), any Error>) -> Void)`](/documentation/CloudKit/CKDatabase/fetch(withQuery:inZoneWith:desiredKeys:resultsLimit:completionHandler:))

Searches for records that match a predicate and delivers them to a completion handler.

[`func fetch(withCursor: CKQueryOperation.Cursor, desiredKeys: [CKRecord.FieldKey]?, resultsLimit: Int, completionHandler: (Result<(matchResults: [(CKRecord.ID, Result<CKRecord, any Error>)], queryCursor: CKQueryOperation.Cursor?), any Error>) -> Void)`](/documentation/CloudKit/CKDatabase/fetch(withCursor:desiredKeys:resultsLimit:completionHandler:))

Retrieves the next batch of records from an existing search and delivers them to a completion handler.

[`func perform(CKQuery, inZoneWith: CKRecordZone.ID?, completionHandler: ([CKRecord]?, (any Error)?) -> Void)`](/documentation/CloudKit/CKDatabase/perform(_:inZoneWith:completionHandler:))

Searches for records matching a predicate in the specified record zone.

[`func records(matching: CKQuery, inZoneWith: CKRecordZone.ID?) async throws -> [CKRecord]`](/documentation/CloudKit/CKDatabase/records(matching:inZoneWith:))

Searches for records in the specified record zone and returns them to an awaiting caller.

### Modifying Records

[`func modifyRecords(saving: [CKRecord], deleting: [CKRecord.ID], savePolicy: CKModifyRecordsOperation.RecordSavePolicy, atomically: Bool) async throws -> (saveResults: [CKRecord.ID : Result<CKRecord, any Error>], deleteResults: [CKRecord.ID : Result<Void, any Error>])`](/documentation/CloudKit/CKDatabase/modifyRecords(saving:deleting:savePolicy:atomically:))

Modifies the specified records and returns the results to an awaiting caller.

[`func modifyRecords(saving: [CKRecord], deleting: [CKRecord.ID], savePolicy: CKModifyRecordsOperation.RecordSavePolicy, atomically: Bool, completionHandler: (Result<(saveResults: [CKRecord.ID : Result<CKRecord, any Error>], deleteResults: [CKRecord.ID : Result<Void, any Error>]), any Error>) -> Void)`](/documentation/CloudKit/CKDatabase/modifyRecords(saving:deleting:savePolicy:atomically:completionHandler:))

Modifies the specified records and delivers the results to a completion handler.

[`enum RecordSavePolicy`](/documentation/CloudKit/CKModifyRecordsOperation/RecordSavePolicy)

Constants that indicate which policy to apply when saving records.

[`func save(CKRecord, completionHandler: (CKRecord?, (any Error)?) -> Void)`](/documentation/CloudKit/CKDatabase/save(_:completionHandler:)-3tatz)

Saves a specific record.

[`func delete(withRecordID: CKRecord.ID, completionHandler: (CKRecord.ID?, (any Error)?) -> Void)`](/documentation/CloudKit/CKDatabase/delete(withRecordID:completionHandler:))

Deletes a specific record.

### Fetching Record Zones

[`func recordZones(for: [CKRecordZone.ID]) async throws -> [CKRecordZone.ID : Result<CKRecordZone, any Error>]`](/documentation/CloudKit/CKDatabase/recordZones(for:))

Fetches the specified record zones and returns them to an awaiting caller.

[`func fetch(withRecordZoneIDs: [CKRecordZone.ID], completionHandler: (Result<[CKRecordZone.ID : Result<CKRecordZone, any Error>], any Error>) -> Void)`](/documentation/CloudKit/CKDatabase/fetch(withRecordZoneIDs:completionHandler:))

Fetches the specified record zones and delivers them to a completion handler.

[`func fetchAllRecordZones(completionHandler: ([CKRecordZone]?, (any Error)?) -> Void)`](/documentation/CloudKit/CKDatabase/fetchAllRecordZones(completionHandler:))

Fetches all record zones from the current database.

[`func fetch(withRecordZoneID: CKRecordZone.ID, completionHandler: (CKRecordZone?, (any Error)?) -> Void)`](/documentation/CloudKit/CKDatabase/fetch(withRecordZoneID:completionHandler:))

Fetches a specific record zone.

### Modifying Record Zones

[`func modifyRecordZones(saving: [CKRecordZone], deleting: [CKRecordZone.ID]) async throws -> (saveResults: [CKRecordZone.ID : Result<CKRecordZone, any Error>], deleteResults: [CKRecordZone.ID : Result<Void, any Error>])`](/documentation/CloudKit/CKDatabase/modifyRecordZones(saving:deleting:))

Modifies the specified record zones and returns the results to an awaiting caller.

[`func modifyRecordZones(saving: [CKRecordZone], deleting: [CKRecordZone.ID], completionHandler: (Result<(saveResults: [CKRecordZone.ID : Result<CKRecordZone, any Error>], deleteResults: [CKRecordZone.ID : Result<Void, any Error>]), any Error>) -> Void)`](/documentation/CloudKit/CKDatabase/modifyRecordZones(saving:deleting:completionHandler:))

Modifies the specified record zones and delivers the results to a completion handler.

[`func save(CKRecordZone, completionHandler: (CKRecordZone?, (any Error)?) -> Void)`](/documentation/CloudKit/CKDatabase/save(_:completionHandler:)-32ffr)

Saves a specific record zone.

[`func delete(withRecordZoneID: CKRecordZone.ID, completionHandler: (CKRecordZone.ID?, (any Error)?) -> Void)`](/documentation/CloudKit/CKDatabase/delete(withRecordZoneID:completionHandler:))

Deletes a specific record zone.

### Fetching Subscriptions

[`func subscriptions(for: [CKSubscription.ID]) async throws -> [CKSubscription.ID : Result<CKSubscription, any Error>]`](/documentation/CloudKit/CKDatabase/subscriptions(for:))

Fetches the specified subscriptions and returns them to an awaiting caller.

[`func fetch(withSubscriptionIDs: [CKSubscription.ID], completionHandler: (Result<[CKSubscription.ID : Result<CKSubscription, any Error>], any Error>) -> Void)`](/documentation/CloudKit/CKDatabase/fetch(withSubscriptionIDs:completionHandler:))

Fetches the specified subscriptions and delivers them to a completion handler.

[`func subscription(for: CKSubscription.ID) async throws -> CKSubscription`](/documentation/CloudKit/CKDatabase/subscription(for:))

Fetches a specific subscription and returns it to an awaiting caller.

[`func fetch(withSubscriptionID: CKSubscription.ID, completionHandler: (CKSubscription?, (any Error)?) -> Void)`](/documentation/CloudKit/CKDatabase/fetch(withSubscriptionID:completionHandler:))

Fetches a specific subscription and delivers it to a completion handler.

[`- (void) fetchSubscriptionWithID:(CKSubscriptionID) subscriptionID completionHandler:(void (^)(CKSubscription *subscription, NSError *error)) completionHandler;`](/documentation/CloudKit/CKDatabase/fetchSubscriptionWithID:completionHandler:)

Fetches a specific subscription and delivers it to a completion handler.

[`func fetchAllSubscriptions(completionHandler: ([CKSubscription]?, (any Error)?) -> Void)`](/documentation/CloudKit/CKDatabase/fetchAllSubscriptions(completionHandler:))

Fetches all subscriptions from the current database.

### Modifying Subscriptions

[`func modifySubscriptions(saving: [CKSubscription], deleting: [CKSubscription.ID]) async throws -> (saveResults: [CKSubscription.ID : Result<CKSubscription, any Error>], deleteResults: [CKSubscription.ID : Result<Void, any Error>])`](/documentation/CloudKit/CKDatabase/modifySubscriptions(saving:deleting:))

Modifies the specified subscriptions and returns the results to an awaiting caller.

[`func modifySubscriptions(saving: [CKSubscription], deleting: [CKSubscription.ID], completionHandler: (Result<(saveResults: [CKSubscription.ID : Result<CKSubscription, any Error>], deleteResults: [CKSubscription.ID : Result<Void, any Error>]), any Error>) -> Void)`](/documentation/CloudKit/CKDatabase/modifySubscriptions(saving:deleting:completionHandler:))

Modifies the specified subscriptions and delivers the results to a completion handler.

[`func save(CKSubscription, completionHandler: (CKSubscription?, (any Error)?) -> Void)`](/documentation/CloudKit/CKDatabase/save(_:completionHandler:)-9pona)

Saves a specific subscription.

[`func deleteSubscription(withID: CKSubscription.ID) async throws -> CKSubscription.ID`](/documentation/CloudKit/CKDatabase/deleteSubscription(withID:))

Deletes a specific subscription and returns the deleted subscription’s identifier to an awaiting caller.

[`func delete(withSubscriptionID: CKSubscription.ID, completionHandler: (String?, (any Error)?) -> Void)`](/documentation/CloudKit/CKDatabase/delete(withSubscriptionID:completionHandler:))

Deletes a specific subscription and delivers the deleted subscription’s identifier to a completion handler.

[`- (void) deleteSubscriptionWithID:(CKSubscriptionID) subscriptionID completionHandler:(void (^)(CKSubscriptionID subscriptionID, NSError *error)) completionHandler;`](/documentation/CloudKit/CKDatabase/deleteSubscriptionWithID:completionHandler:)

Deletes a specific subscription and delivers the deleted subscription’s identifier to a completion handler.

### Fetching Changes

[`func databaseChanges(since: CKServerChangeToken?, resultsLimit: Int?) async throws -> (modifications: [CKDatabase.DatabaseChange.Modification], deletions: [CKDatabase.DatabaseChange.Deletion], changeToken: CKServerChangeToken, moreComing: Bool)`](/documentation/CloudKit/CKDatabase/databaseChanges(since:resultsLimit:))

Fetches all modified record zones and returns them to an awaiting caller.

[`func fetchDatabaseChanges(since: CKServerChangeToken?, resultsLimit: Int?, completionHandler: (Result<(modifications: [CKDatabase.DatabaseChange.Modification], deletions: [CKDatabase.DatabaseChange.Deletion], changeToken: CKServerChangeToken, moreComing: Bool), any Error>) -> Void)`](/documentation/CloudKit/CKDatabase/fetchDatabaseChanges(since:resultsLimit:completionHandler:))

Fetches all modified record zones and delivers them to a completion handler.

[`enum DatabaseChange`](/documentation/CloudKit/CKDatabase/DatabaseChange)

Objects that indicate the type of database change.

[`func recordZoneChanges(inZoneWith: CKRecordZone.ID, since: CKServerChangeToken?, desiredKeys: [CKRecord.FieldKey]?, resultsLimit: Int?) async throws -> (modificationResultsByID: [CKRecord.ID : Result<CKDatabase.RecordZoneChange.Modification, any Error>], deletions: [CKDatabase.RecordZoneChange.Deletion], changeToken: CKServerChangeToken, moreComing: Bool)`](/documentation/CloudKit/CKDatabase/recordZoneChanges(inZoneWith:since:desiredKeys:resultsLimit:))

Fetches all modified records from a specific record zone and returns them to an awaiting caller.

[`func fetchRecordZoneChanges(inZoneWith: CKRecordZone.ID, since: CKServerChangeToken?, desiredKeys: [CKRecord.FieldKey]?, resultsLimit: Int?, completionHandler: (Result<(modificationResultsByID: [CKRecord.ID : Result<CKDatabase.RecordZoneChange.Modification, any Error>], deletions: [CKDatabase.RecordZoneChange.Deletion], changeToken: CKServerChangeToken, moreComing: Bool), any Error>) -> Void)`](/documentation/CloudKit/CKDatabase/fetchRecordZoneChanges(inZoneWith:since:desiredKeys:resultsLimit:completionHandler:))

Fetches all modified records from a specific record zone and delivers them to a completion handler.

[`enum RecordZoneChange`](/documentation/CloudKit/CKDatabase/RecordZoneChange)

Objects that indicate the type of record zone change.

### Running Operations

[`func add(CKDatabaseOperation)`](/documentation/CloudKit/CKDatabase/add(_:))

Executes the specified operation in the current database.

### Getting the Database Type

[`var databaseScope: CKDatabase.Scope`](/documentation/CloudKit/CKDatabase/databaseScope)

The type of database.

[`enum Scope`](/documentation/CloudKit/CKDatabase/Scope)

Constants that represent the scope of a database.

### Instance Methods

[`func allRecordZones() async throws -> [CKRecordZone]`](/documentation/CloudKit/CKDatabase/allRecordZones())

Fetches all record zones from the current database.

[`func allSubscriptions() async throws -> [CKSubscription]`](/documentation/CloudKit/CKDatabase/allSubscriptions())

Fetches all subscriptions from the current database.

[`func deleteRecord(withID: CKRecord.ID) async throws -> CKRecord.ID`](/documentation/CloudKit/CKDatabase/deleteRecord(withID:))

Deletes a specific record.

[`func deleteRecordZone(withID: CKRecordZone.ID) async throws -> CKRecordZone.ID`](/documentation/CloudKit/CKDatabase/deleteRecordZone(withID:))

Deletes a specific record zone.

[`func perform(CKQuery, inZoneWith: CKRecordZone.ID?) async throws -> [CKRecord]`](/documentation/CloudKit/CKDatabase/perform(_:inZoneWith:))

Searches for records matching a predicate in the specified record zone.

[`func record(for: CKRecord.ID) async throws -> CKRecord`](/documentation/CloudKit/CKDatabase/record(for:))

Fetches a specific record.

[`func recordZone(for: CKRecordZone.ID) async throws -> CKRecordZone`](/documentation/CloudKit/CKDatabase/recordZone(for:))

Fetches a specific record zone.

[`func save(CKRecord) async throws -> CKRecord`](/documentation/CloudKit/CKDatabase/save(_:)-1j6fq)

Saves a specific record.

[`func save(CKSubscription) async throws -> CKSubscription`](/documentation/CloudKit/CKDatabase/save(_:)-69wq8)

Saves a specific subscription.

[`func save(CKRecordZone) async throws -> CKRecordZone`](/documentation/CloudKit/CKDatabase/save(_:)-7btlo)

Saves a specific record zone.

## Relationships

### Conforms To

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`CVarArg`](/documentation/Swift/CVarArg)

[`Sendable`](/documentation/Swift/Sendable)

[`Hashable`](/documentation/Swift/Hashable)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`Equatable`](/documentation/Swift/Equatable)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)