Using Core Data, Icloud and Cloudkit for Syncing and Backup and How It Works Together

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

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.

Is Core Data necessary for across device data synchronization with iCloud?

CloudKit is transport only. Core Data is local persistence only. You can use them together, but you can also use them separately. If you want to use CloudKit but not Core Data, you would need to write your own code to handle local persistence. You might, for example, use plain SQLite, with a wrapper like GRDB for local persistence. Or some other way. There are lots of ways to store local data on apps. However, NSPersistentCloudKitContainer is part of Core Data (that’s what makes it work with CloudKit), so you would not use that.

CoreData with CloudKit - data not syncing over iCloud

I just want to add an answer if anyone would have similar problem. What worked for me was signing out from iCloud and signing back in. That's it. My code was OK, it was some problem with iCloud itself.

It may not work for you, but if your code looks good and you don't have a clue what could be the problem, try to sign out and sign in to the account that has problem with syncing.



Related Topics



Leave a reply



Submit