Using Core Data, iCloud and CloudKit for syncing and backup and how it works together

Yan picture Yan · Jun 27, 2014 · Viewed 28.6k times · Source

I am in the early stages of creating an app where I would like to save, sync and backup data. The app will not store any files just data in a database. It is going to be iOS 8 and up so I am able to use CloudKit. I did some research and still not clear on how Core Data, iCloud and CloudKit work together.

As far as understand CloudKit is just a way of getting and retrieving data to/from the cloud. Is CloudKit just a different way of syncing data with iCloud?

My questions are:

  1. If I do use CloudKit, do I still need to create local core data database?

    • If yes will it be automatically synced with iCloud or I would have to call methods to store to both places?
  2. If the data is only stored in the cloud will user be able to access it when iOS device is not connected to the internet. I read that CloudKit will have only limited caching.

  3. How will that work if iCloud account is not enabled.

If someone can kind of break down what each technology does in the process of saving and syncing core data database offline and online.

My current understanding is:

  • Core Data is used to store data locally

  • iCloud syncs the data and stores in the cloud

  • CloudKit gives the ability to store and manage data in the cloud??

I hope I provided enough info for this question not to get closed.

Answer

Tom Harrington picture Tom Harrington · Jun 27, 2014

It's like this:

  • Core Data on its own, is completely local and does not automatically work with any of Apple's cloud services.
  • Core Data with iCloud enabled turns on syncing via iCloud. Any changes you save in Core Data are propagated to the cloud, and any changes made in the cloud are automatically downloaded. The data is stored both in iCloud and in a local persistent store file, so it's available even when the device is offline. You don't have to write any cloud-specific code, you just need to add listening for incoming changes (which is a lot like changes made on a different managed object context).
  • CloudKit is not related to Core Data. It's not a sync system, it's a transfer system. Meaning that any time you want to read/write cloud data, you need to make explicit CloudKit API calls to do so. Nothing happens automatically. CloudKit does not store data on the device, so the data is not available if the device is offline. CloudKit also adds some features not available to Core Data with iCloud-- like public shared data and the ability to download only part of the data set instead of the whole thing.

If you wanted to use CloudKit with Core Data, you'd have to write your own custom code to translate between managed objects and CloudKit records. It's not impossible, but it's more code to write. It might be more reliable but it's too soon to say for sure.

I wrote a blog post describing CloudKit from the perspective of someone who's used Core Data and iCloud in the past.

Update, June 2016: As of the most recent documentation for NSPersistentStoreCoordinator, everything related to Core Data with iCloud is marked as deprecated. As a result it should probably be avoided for new development.