Class

CKOperation

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

Declaration

@interface CKOperation : NSOperation

Overview

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 longLived property to YES, set the operation completion block, and run it. Later, get the long-lived operation identifiers using the fetchAllLongLivedOperationIDsWithCompletionHandler: method in the CKContainer class. To get the associated operation object, use the fetchLongLivedOperationWithID: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 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];
        }];
    }
}];

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.

Topics

Accessing the Container

container

The container to use for the operation.

Deprecated

Customizing the Network Access Behavior

allowsCellularAccess

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

Deprecated
timeoutIntervalForRequest

The timeout interval to use when waiting for additional data.

Deprecated
timeoutIntervalForResource

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

Deprecated

Creating Long-Lived Operations

longLived

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

Deprecated
longLivedOperationWasPersistedBlock

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

operationID

A unique identifier for a long-lived operation.

Creating an Operation

Relationships

Inherits From

See Also

Base Classes

CKDatabaseOperation

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

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software