About Using iCloud with Core Data

iCloud is a cloud service that gives your users a consistent and seamless experience across all of their iCloud-enabled devices. iCloud works with ubiquity containers—special folders that your app stores data in—to manage your app’s cloud storage. When you add, delete, or make changes to a file in your app’s ubiquity container, the system uploads the changes to iCloud. Other peers download the changes to keep your app up to date.

To help you persist managed objects to the cloud, iCloud is integrated with Core Data. To use Core Data with iCloud, you simply tell Core Data to create an iCloud-enabled persistent store. The iCloud service and Core Data take care of the rest: The system manages the files in the ubiquity container that make up your persistent store, and Core Data helps you keep your app up to date. To let you know when the content in your container changes, Core Data posts notifications.

At a Glance

When you use Core Data, you have several storage models to choose from. Using Core Data with iCloud, you have a subset of these options, as follows:

When you decide on a storage model, consider the strengths of each store as well as the iCloud-specific strengths discussed below.

Use Core Data Atomic Stores for Small, Simple Storage

iCloud supports XML (OS X only) and binary atomic persistent stores. Useful for small, simple storage requirements, Core Data’s atomic-store support sacrifices merging and network efficiency for simplicity of use for when your data rarely changes. When you use iCloud with an atomic persistent store, you work directly in the ubiquity container. Binary (and XML) store files are themselves transferred to the iCloud servers; so whenever a change is made to the data, the system uploads the entire store and pushes it to all connected devices. This means that changes on one peer can overwrite changes made on the others.

iCloud treats Core Data atomic stores like any other file added to your app’s ubiquity container. You can learn more about managing files in your app’s ubiquity container in iCloud Design Guide.

Use Core Data Transactional Stores for Large, Complex Storage

Core Data provides ubiquitous persistent storage for SQLite-backed stores. Core Data takes advantage of the SQLite transactional persistence mechanism, saving and retrieving transaction logs—logs of changes—in your app’s ubiquity container. The Core Data framework’s reliability and performance extend to iCloud, resulting in dependable, fault-tolerant storage across multiple peers. Continue reading this document to learn more about how to use iCloud with an SQLite store.

(iOS Only) Use Core Data Document Stores to Manage Documents in iCloud

The UIManagedDocument class is the primary mechanism through which Core Data stores managed documents in iCloud on iOS. The UIManagedDocument class manages the entire Core Data stack for each document in a document-based app. Changes to managed documents are automatically persisted to iCloud. By default, managed documents are backed by SQLite-type persistent stores, but you can choose to use atomic stores instead. While the steps you take to integrate the UIManagedDocument class into your app differ, the model-specific guidelines and best practices you follow are generally the same. You can find additional implementation strategies and tips in Using Document Storage with iCloud.

Prerequisites

iCloud is a service that stores your app’s data in the cloud and makes it available to your users’ iCloud-enabled devices. Before using Core Data’s iCloud integration, you should read more about iCloud in iCloud Design Guide. In addition, this guide assumes a working knowledge of Core Data, a powerful object graph and data persistence framework. For more information about the Core Data framework, see Introduction to Core Data Programming Guide in Core Data Programming Guide.