<!--
{
  "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/CKOperation",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "CloudKit"
    ],
    "preciseIdentifier" : "c:objc(cs)CKOperation"
  },
  "title" : "CKOperation"
}
-->

# CKOperation

The abstract base class for all operations that execute in a database.

```
class CKOperation
```

## Overview

All CloudKit operations descend from `CKOperation`, which provides the infrastructure for executing tasks in one of your app’s containers. Don’t subclass or create instances of this class directly. Instead, create instances of one of its concrete subclasses.

Use the properties of this class to configure the behavior of the operation before submitting it to a queue or executing it directly. CloudKit operations involve communicating with the iCloud servers to send and receive data. You can use the properties of this class to configure the behavior of those network requests to ensure the best performance for your app.

> Important: `CKOperation` objects have a default quality of service level of <doc://com.apple.documentation/documentation/Foundation/QualityOfService/default> (see <doc://com.apple.documentation/documentation/Foundation/Operation/qualityOfService>). Operations with this service level are discretionary, and the system schedules them for an optimal time according to battery level and other factors. On iPhone, discretionary activities pause when the device is in Low Power Mode. For information about quality of service levels, see [Prioritize Work with Quality of Service Classes](https://developer.apple.com/library/archive/documentation/Performance/Conceptual/EnergyGuide-iOS/PrioritizeWorkWithQoS.html#//apple_ref/doc/uid/TP40015243-CH39) in [Energy Efficiency Guide for iOS Apps](https://developer.apple.com/library/archive/documentation/Performance/Conceptual/EnergyGuide-iOS/index.html#//apple_ref/doc/uid/TP40015243) and [Prioritize Work at the Task Level](https://developer.apple.com/library/archive/documentation/Performance/Conceptual/power_efficiency_guidelines_osx/PrioritizeWorkAtTheTaskLevel.html#//apple_ref/doc/uid/TP40013929-CH35) in [Energy Efficiency Guide for Mac Apps](https://developer.apple.com/library/archive/documentation/Performance/Conceptual/power_efficiency_guidelines_osx/index.html#//apple_ref/doc/uid/TP40013929).

### Long-Lived Operations

A *long-lived operation* is an operation that continues to run after the user closes the app. To specify a long-lived operation, set [`isLongLived`](/documentation/CloudKit/CKOperation/isLongLived) to <doc://com.apple.documentation/documentation/Swift/true>, provide a completion handler, and execute the operation. To get the identifiers of all running long-lived operations, use the [`allLongLivedOperationIDs()`](/documentation/CloudKit/CKContainer/allLongLivedOperationIDs()) method that [`CKContainer`](/documentation/CloudKit/CKContainer) provides. To get a specific long-lived operation, use the [`longLivedOperation(for:)`](/documentation/CloudKit/CKContainer/longLivedOperation(for:)) method. Make sure you set the completion handler of a long-lived operation before you execute it so that the system can notify you when it completes and you can process the results. Do not execute an operation, change it to long-lived, and execute it again as a long-lived operation.

```objc
[container fetchAllLongLivedOperationIDsWithCompletionHandler:^(NSArray<NSString *> *_Nullable operationIDs, NSError *_Nullable error) {
    if (error) {
        // Handle error
        return
    }
    for (NSString *operationID in operationIDs) {
        [container fetchLongLivedOperationWithID:operationID completionHandler:^(CKOperation *_Nullable operation, NSError *_Nullable error) {
            if (error) {
                // Handle error
                return
            }
            // Add callback handlers to operation
            [container addOperation:operation];
        }];
    }
}];
```

The following is the typical life cycle of a long-lived operation:

1. The app creates a long-lived operation and executes it.
   The daemon starts saving and sending the callbacks to the running app.
2. The app exits.
   The daemon continues running the long-lived operation and saves the callbacks.
3. The app launches and fetches the long-lived operation.
   If the operation is running or if it completed within the previous 24 hours, the daemon returns a proxy for the long-lived operation. If the operation completed more than 24 hours previously, the daemon may stop returning it in fetch requests.
4. The app runs the long-lived operation again.
   The daemon sends the app all the saved callbacks (it doesn’t actually rerun the operation), and continues saving the callbacks and sending them to the running app.
5. The app receives the completion callback or the app cancels the operation.
   The daemon stops including the operation in future fetch results.

## Topics

### Creating an Operation

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

Creates an operation.

### Identifying the Operation

[`operationID`](/documentation/CloudKit/CKOperation/operationID-8auuc)

A unique identifier for a long-lived operation.

[`CKOperation.ID`](/documentation/CloudKit/CKOperation/ID)

A type that represents the ID of an operation.

[`operationID`](/documentation/CloudKit/CKOperation/operationID-3eujz)

A unique identifier for a long-lived operation.

[`CKOperationID`](/documentation/CloudKit/CKOperationID)

A type that represents the ID of an operation.

### Managing the Operation’s Configuration

[`configuration`](/documentation/CloudKit/CKOperation/configuration-swift.property)

The operation’s configuration.

[`CKOperation.Configuration`](/documentation/CloudKit/CKOperation/Configuration-swift.class)

An object that describes how a CloudKit operation behaves.

[`group`](/documentation/CloudKit/CKOperation/group)

The operation’s group.

[`longLivedOperationWasPersistedBlock`](/documentation/CloudKit/CKOperation/longLivedOperationWasPersistedBlock)

The closure to execute when the server begins to store callbacks for the long-lived operation.

### Deprecated

[Deprecated Symbols](/documentation/CloudKit/ckoperation-deprecated-symbols)

Review unsupported symbols and their replacements.



---

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)