I’m just wondering the least steps needed to use iCloud with Core Data to sync data among different devices. So at the very beginning, I should build a rough app using Core Data to store the data. For simplicity, I use the Core Data templete provided by Xcode, which is not recommended in many tutorials.

There are only two steps to achieve this:

First, set the options when adding persistent store to a coordinator.

The code provided by the templete to add persistent store is:

try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil)

Replace it by:

let options = [NSPersistentStoreUbiquitousContentNameKey: "iCloudAndCoredata"]
try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: options)

That’s all the code needed.

Second, turn on the iCloud, choose an account and check iCloud Documents:

image

image

image

All done. The demo project is here.

This article just makes iCloud work with CoreData. There are still a lot of things to do to make a great iCloud-sync app. For example, in the demo, if you want to see the sycn effect, you should restart the app, which is not bearable in a real app.