<!--
{
  "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

[`configuredWith(configuration:group:body:)`](/documentation/CloudKit/CKDatabase/configuredWith(configuration:group:body:)-637p1)

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

[`configuredWith(configuration:group:body:)`](/documentation/CloudKit/CKDatabase/configuredWith(configuration:group:body:)-12vrs)

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

### Fetching Records

[`records(for:desiredKeys:)`](/documentation/CloudKit/CKDatabase/records(for:desiredKeys:))

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

[`fetch(withRecordIDs:desiredKeys:completionHandler:)`](/documentation/CloudKit/CKDatabase/fetch(withRecordIDs:desiredKeys:completionHandler:))

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

[`fetch(withRecordID:completionHandler:)`](/documentation/CloudKit/CKDatabase/fetch(withRecordID:completionHandler:))

Fetches a specific record.

### Querying Records

[`records(matching:inZoneWith:desiredKeys:resultsLimit:)`](/documentation/CloudKit/CKDatabase/records(matching:inZoneWith:desiredKeys:resultsLimit:))

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

[`records(continuingMatchFrom:desiredKeys:resultsLimit:)`](/documentation/CloudKit/CKDatabase/records(continuingMatchFrom:desiredKeys:resultsLimit:))

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

[`fetch(withQuery:inZoneWith:desiredKeys:resultsLimit:completionHandler:)`](/documentation/CloudKit/CKDatabase/fetch(withQuery:inZoneWith:desiredKeys:resultsLimit:completionHandler:))

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

[`fetch(withCursor:desiredKeys:resultsLimit:completionHandler:)`](/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.

[`perform(_:inZoneWith:completionHandler:)`](/documentation/CloudKit/CKDatabase/perform(_:inZoneWith:completionHandler:))

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

[`records(matching:inZoneWith:)`](/documentation/CloudKit/CKDatabase/records(matching:inZoneWith:))

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

### Modifying Records

[`modifyRecords(saving:deleting:savePolicy:atomically:)`](/documentation/CloudKit/CKDatabase/modifyRecords(saving:deleting:savePolicy:atomically:))

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

[`modifyRecords(saving:deleting:savePolicy:atomically:completionHandler:)`](/documentation/CloudKit/CKDatabase/modifyRecords(saving:deleting:savePolicy:atomically:completionHandler:))

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

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

Constants that indicate which policy to apply when saving records.

[`save(_:completionHandler:)`](/documentation/CloudKit/CKDatabase/save(_:completionHandler:)-3tatz)

Saves a specific record.

[`delete(withRecordID:completionHandler:)`](/documentation/CloudKit/CKDatabase/delete(withRecordID:completionHandler:))

Deletes a specific record.

### Fetching Record Zones

[`recordZones(for:)`](/documentation/CloudKit/CKDatabase/recordZones(for:))

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

[`fetch(withRecordZoneIDs:completionHandler:)`](/documentation/CloudKit/CKDatabase/fetch(withRecordZoneIDs:completionHandler:))

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

[`fetchAllRecordZones(completionHandler:)`](/documentation/CloudKit/CKDatabase/fetchAllRecordZones(completionHandler:))

Fetches all record zones from the current database.

[`fetch(withRecordZoneID:completionHandler:)`](/documentation/CloudKit/CKDatabase/fetch(withRecordZoneID:completionHandler:))

Fetches a specific record zone.

### Modifying Record Zones

[`modifyRecordZones(saving:deleting:)`](/documentation/CloudKit/CKDatabase/modifyRecordZones(saving:deleting:))

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

[`modifyRecordZones(saving:deleting:completionHandler:)`](/documentation/CloudKit/CKDatabase/modifyRecordZones(saving:deleting:completionHandler:))

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

[`save(_:completionHandler:)`](/documentation/CloudKit/CKDatabase/save(_:completionHandler:)-32ffr)

Saves a specific record zone.

[`delete(withRecordZoneID:completionHandler:)`](/documentation/CloudKit/CKDatabase/delete(withRecordZoneID:completionHandler:))

Deletes a specific record zone.

### Fetching Subscriptions

[`subscriptions(for:)`](/documentation/CloudKit/CKDatabase/subscriptions(for:))

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

[`fetch(withSubscriptionIDs:completionHandler:)`](/documentation/CloudKit/CKDatabase/fetch(withSubscriptionIDs:completionHandler:))

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

[`subscription(for:)`](/documentation/CloudKit/CKDatabase/subscription(for:))

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

[`fetch(withSubscriptionID:completionHandler:)`](/documentation/CloudKit/CKDatabase/fetch(withSubscriptionID:completionHandler:))

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

[`fetchSubscriptionWithID:completionHandler:`](/documentation/CloudKit/CKDatabase/fetchSubscriptionWithID:completionHandler:)

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

[`fetchAllSubscriptions(completionHandler:)`](/documentation/CloudKit/CKDatabase/fetchAllSubscriptions(completionHandler:))

Fetches all subscriptions from the current database.

### Modifying Subscriptions

[`modifySubscriptions(saving:deleting:)`](/documentation/CloudKit/CKDatabase/modifySubscriptions(saving:deleting:))

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

[`modifySubscriptions(saving:deleting:completionHandler:)`](/documentation/CloudKit/CKDatabase/modifySubscriptions(saving:deleting:completionHandler:))

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

[`save(_:completionHandler:)`](/documentation/CloudKit/CKDatabase/save(_:completionHandler:)-9pona)

Saves a specific subscription.

[`deleteSubscription(withID:)`](/documentation/CloudKit/CKDatabase/deleteSubscription(withID:))

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

[`delete(withSubscriptionID:completionHandler:)`](/documentation/CloudKit/CKDatabase/delete(withSubscriptionID:completionHandler:))

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

[`deleteSubscriptionWithID:completionHandler:`](/documentation/CloudKit/CKDatabase/deleteSubscriptionWithID:completionHandler:)

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

### Fetching Changes

[`databaseChanges(since:resultsLimit:)`](/documentation/CloudKit/CKDatabase/databaseChanges(since:resultsLimit:))

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

[`fetchDatabaseChanges(since:resultsLimit:completionHandler:)`](/documentation/CloudKit/CKDatabase/fetchDatabaseChanges(since:resultsLimit:completionHandler:))

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

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

Objects that indicate the type of database change.

[`recordZoneChanges(inZoneWith:since:desiredKeys:resultsLimit:)`](/documentation/CloudKit/CKDatabase/recordZoneChanges(inZoneWith:since:desiredKeys:resultsLimit:))

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

[`fetchRecordZoneChanges(inZoneWith:since:desiredKeys:resultsLimit:completionHandler:)`](/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.

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

Objects that indicate the type of record zone change.

### Running Operations

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

Executes the specified operation in the current database.

### Getting the Database Type

[`databaseScope`](/documentation/CloudKit/CKDatabase/databaseScope)

The type of database.

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

Constants that represent the scope of a database.



---

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)