Article

Mirroring a Core Data Store with CloudKit

Back user interfaces with a local replica of a CloudKit private database.

Overview

Use Core Data with CloudKit to give users seamless access to the data in your app across all their devices.

On its own, Core Data provides powerful object graph management features for developing an app with structured data. CloudKit lets users access their data across every device on their iCloud account, while serving as an always-available backup service. Core Data with CloudKit combines the benefits of local persistence with cloud backup and distribution.

Flow diagram showing a record syncing between CloudKit and three devices: a laptop, an iPad, and an iPhone.

Determine If Your App Is Eligible for Core Data with CloudKit

Apps adopting Core Data or currently using Core Data may use Core Data with CloudKit as long as the persistent store is an NSSQLiteStoreType store, and the data model meets the requirements described in Design a CloudKit-Compatible Core Data Model.

Core Data with CloudKit doesn’t support existing CloudKit containers. To fully manage all aspects of data mirroring, Core Data owns the CloudKit schema created from the Core Data model. Existing CloudKit containers aren’t compatible with this schema. If your app already uses CloudKit, you can add Core Data with CloudKit when synchronizing a Core Data Store with a new container. For more information about working with multiple stores, see Manage Multiple Stores.

Set Up Your Development Environment

You need an Apple Developer Program account to access the CloudKit service and your team’s CloudKit containers, during development. Enroll in the Apple Developer Program.

Additionally, for development you need an iCloud account to save records to a CloudKit container. Core Data with CloudKit uses a specific record zone in the CloudKit private database, which is accessible only to the current user. If you don’t have an iCloud account, create one that you can use during development. See Create an iCloud Account for Development.

You can run and test Core Data with CloudKit apps using Simulator. You may also test with multiple physical devices logged into the same iCloud account on a good wireless internet connection.

Configure Core Data with CloudKit

To begin, add the CloudKit service and related capabilities to your app. Modify your Xcode project, and your stack setup, as described in Setting Up Core Data with CloudKit.

Next, design your Core Data model, and use it to initialize the CloudKit schema, as described in Creating a Core Data Model for CloudKit.

As you develop your app, it’s important to isolate the current view from store changes, and to update it when necessary. For information about the life cycle of uploads and downloads, its impact on your views, and troubleshooting tips, see Syncing a Core Data Store with CloudKit.

Finally, if you are building custom features or writing a web app, discover how to work with the generated CloudKit schema in Reading CloudKit Records for Core Data.

Topics

Configuring CloudKit Mirroring

Setting Up Core Data with CloudKit

Set up the classes and capabilities that sync your store to CloudKit.

Creating a Core Data Model for CloudKit

Design a CloudKit-compatible data model and initialize your CloudKit schema.

Syncing a Core Data Store with CloudKit

Synchronize objects between devices, and handle store changes in the user interface.

Reading CloudKit Records for Core Data

Access CloudKit records created from Core Data managed objects.

See Also

CloudKit Mirroring

Synchronizing a Local Store to the Cloud

Share data between a user’s devices using Core Data with CloudKit.

class NSPersistentCloudKitContainer

A container that encapsulates the Core Data stack in your app and mirrors select persistent stores to a CloudKit private database.

Beta
class NSPersistentCloudKitContainerOptions

An object that customizes how a store description aligns with a CloudKit database.

Beta