The abstract base class for all operations that can be executed against a CloudKit database.


class CKOperation : Operation


All CloudKit operation objects descend from CKOperation, which provides the infrastructure for executing tasks against one of your app’s containers. Do not 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 object before submitting it to an operation queue or executing it directly. CloudKit operations involve communicating with the iCloud servers to send and receive data. The properties of this class let you configure the behavior of those network operations to ensure the best performance for your app.

Long-Lived Operations

A long-lived operation is an operation that continues to run after the app exits. To specify a long-lived operation, set the isLongLived property to true, set the operation completion block, and run it. Later, get the long-lived operation identifiers using the fetchAllLongLivedOperationIDs(completionHandler:) method in the CKContainer class. To get the associated operation object, use the fetchLongLivedOperation(withID:completionHandler:) method. Then set the completion block of the long-lived operation so that your app instance receives the callbacks. Otherwise, your app won’t be notified when a long-lived operation completes and won’t be able to process the results.

container.fetchAllLongLivedOperationIDs(completionHandler: { (operationIDs, error) in
    if let error = error {
        print("Error fetching long lived operations: \(error)")
        // Handle error
    guard let identifiers = operationIDs else { return }
    for operationID in identifiers {
        container.fetchLongLivedOperation(withID: operationID, completionHandler: { (operation, error) in
            if let error = error {
                print("Error fetching operation: \(operationID)\n\(error)")
                // Handle error
            guard let operation = operation else { return }
            // Add callback handlers to operation

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

  1. The app creates a long-lived operation and runs 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 less than 24 hours ago, the daemon returns a proxy for the long-lived operation. If the operation completed over 24 hours ago, 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 re-run 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.


Accessing the Container

Customizing the Network Access Behavior

var allowsCellularAccess: Bool

A Boolean value indicating whether the operation object may send data over the cell network.

var timeoutIntervalForRequest: TimeInterval

The timeout interval to use when waiting for additional data.

var timeoutIntervalForResource: TimeInterval

The maximum amount of time that a resource request should be allowed to take.


Creating Long-Lived Operations

var isLongLived: Bool

A Boolean value indicating whether the operation is long-lived.

var longLivedOperationWasPersistedBlock: (() -> Void)?

The block to execute when the server starts storing callbacks for this long-lived operation.

Creating an Operation


class CKOperation.Configuration

A collection of properties that describes how a CloudKit operation should behave.


Inherits From

Conforms To

See Also

Base Classes

class CKDatabaseOperation

The abstract parent class for operations that act on databases in CloudKit.