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

# CKFetchDatabaseChangesOperation

An operation that fetches database changes.

```
class CKFetchDatabaseChangesOperation
```

## Overview

Use this operation to fetch all record zone changes in a database. This includes new record zones, changed zones — including deleted or purged zones — and zones that contain record changes. When you create the operation, you provide a server change token, which is an opaque token that represents a specific point in the database’s history. CloudKit returns only the changes that occur after that point. For your app’s first fetch, or to refetch every change in the database’s history, use `nil` instead.

> Note: Only private and shared databases support this operation. If you attempt to execute this operation in the public database, CloudKit returns an error.

The operation yields new change tokens during its execution, and issues a final change token when it completes without error. The change tokens conform to <doc://com.apple.documentation/documentation/Foundation/NSSecureCoding> and are safe to cache on-disk. This operation’s tokens aren’t compatible with [`CKFetchRecordZoneChangesOperation`](/documentation/CloudKit/CKFetchRecordZoneChangesOperation) so you should segregate them in your cache. Don’t infer any behavior or order from the tokens’ contents.

When your app launches for the first time, use this operation to fetch all the database’s changes. Cache the results on-device and use [`CKDatabaseSubscription`](/documentation/CloudKit/CKDatabaseSubscription) to subscribe to future changes. Fetch those changes on receipt of the push notifications the subscription generates. It’s not necessary to perform a fetch each time your app launches, or to schedule fetches at regular intervals.

The operation calls [`recordZoneWithIDChangedBlock`](/documentation/CloudKit/CKFetchDatabaseChangesOperation/recordZoneWithIDChangedBlock) for each zone that contains record changes. It also calls it for new and modified record zones. Store the IDs that CloudKit provides to this callback. Use those IDs with [`CKFetchRecordZoneChangesOperation`](/documentation/CloudKit/CKFetchRecordZoneChangesOperation) to fetch the corresponding changes. There are similar callbacks for deleted and purged record zones.

To run the operation, add it to the corresponding database’s operation queue. The operation executes its callbacks on a private serial queue.

The following example shows how to create the operation, configure its callbacks, and execute it. For brevity, it omits the delete, and purge callbacks.

```swift
// Create a fetch operation using the server change
// token from the previous fetch.
CKFetchDatabaseChangesOperation *operation =
    [[CKFetchDatabaseChangesOperation alloc]
     initWithPreviousServerChangeToken:token];

// Collect the IDs of the modified record zones.
operation.recordZoneWithIDChangedBlock = ^(CKRecordZoneID *recordZoneID) {
    [recordZoneIDs addObject:recordZoneID];
};

// Process any change tokens that CloudKit provides
// as the operation runs.
operation.changeTokenUpdatedBlock = ^(CKServerChangeToken *newToken) {
    tokenHandler(newToken);
};

// Store the final change token and pass the IDs of the
// modified record zones for further processing.
operation.fetchDatabaseChangesCompletionBlock =
    ^(CKServerChangeToken *newToken, BOOL more, NSError *error) {
    if (error) {
        // Handle the error.
    } else {
        tokenHandler(newToken);
        recordZonesHandler(recordZoneIDs);
    }
};

// Set an appropriate QoS and add the operation to the shared
// database's operation queue to execute it.
operation.qualityOfService = NSQualityOfServiceUtility;
[CKContainer.defaultContainer.sharedCloudDatabase addOperation:operation];
```

## Topics

### Creating an Operation

[`init(previousServerChangeToken:)`](/documentation/CloudKit/CKFetchDatabaseChangesOperation/init(previousServerChangeToken:))

Creates an operation for fetching database changes.

[`init()`](/documentation/CloudKit/CKFetchDatabaseChangesOperation/init())

Creates an empty fetch database changes operation.

### Configuring the Operation

[`fetchAllChanges`](/documentation/CloudKit/CKFetchDatabaseChangesOperation/fetchAllChanges)

A Boolean value that indicates whether to send repeated requests to the server.

[`previousServerChangeToken`](/documentation/CloudKit/CKFetchDatabaseChangesOperation/previousServerChangeToken)

The server change token.

[`resultsLimit`](/documentation/CloudKit/CKFetchDatabaseChangesOperation/resultsLimit)

The maximum number of results that the operation fetches.

### Processing the Operation’s Results

[`recordZoneWithIDChangedBlock`](/documentation/CloudKit/CKFetchDatabaseChangesOperation/recordZoneWithIDChangedBlock)

The closure to execute with a single record zone change.

[`recordZoneWithIDWasDeletedBlock`](/documentation/CloudKit/CKFetchDatabaseChangesOperation/recordZoneWithIDWasDeletedBlock)

The closure to execute when a record zone no longer exists.

[`recordZoneWithIDWasDeletedDueToUserEncryptedDataResetBlock`](/documentation/CloudKit/CKFetchDatabaseChangesOperation/recordZoneWithIDWasDeletedDueToUserEncryptedDataResetBlock)

The closure to execute when a user-invoked account reset deletes a record zone.

[`recordZoneWithIDWasPurgedBlock`](/documentation/CloudKit/CKFetchDatabaseChangesOperation/recordZoneWithIDWasPurgedBlock)

The closure to execute when CloudKit purges a record zone.

[`changeTokenUpdatedBlock`](/documentation/CloudKit/CKFetchDatabaseChangesOperation/changeTokenUpdatedBlock)

The closure to execute when the change token updates.

[`fetchDatabaseChangesCompletionBlock`](/documentation/CloudKit/CKFetchDatabaseChangesOperation/fetchDatabaseChangesCompletionBlock)

The closure to execute when the operation finishes.



---

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)