118

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.

ICL1901
  • 7,632
  • 14
  • 90
  • 138
Yan
  • 3,533
  • 4
  • 24
  • 45
  • 2
    Even i didn't did that much research on it, But as much i understood its don't store in local. Its something like normal server client service. Thats why without internet you can't access your DB. and as per Doc if user didn't enabled icloud account at that time u can only read the data from public container – Sachin Jun 27 '14 at 06:18
  • @Yan can we do like this please suggest, http://stackoverflow.com/questions/25600556/cloudkit-can-we-edit-data-of-public-database – Nikunj Sep 01 '14 at 07:17

2 Answers2

192

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.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
  • Thank you very much for replying. This is exactly what i was looking for. I am just wondering if there is a some kind of corruption of data the local store is that going to be replicated in the cloud? It would be safer to store data in both locally and cloudkit but as you said would be a pain to sync and manage. – Yan Jun 30 '14 at 19:15
  • 1
    In the past I've had trouble with corruption when using Core Data with iCloud, but I haven't used it much since iOS 7 came out. It's supposed to be much better. With CloudKit it's too soon to say, since it's still not officially released. – Tom Harrington Jun 30 '14 at 21:42
  • @TomHarrington can we do like this please suggest, http://stackoverflow.com/questions/25600556/cloudkit-can-we-edit-data-of-public-database – Nikunj Sep 01 '14 at 07:18
  • 3
    @Tom Harrington - Could you try Core Data with iCloud for iOS 8 and let us know your impression? I've experienced lots of corruption in iOS 7 too, and the Apple support confirmed many bugs. I'm in hope that iOS 8 brings its reliability to an acceptable level, but would love to hear from you, the expert ;) – hyouuu Sep 10 '14 at 19:06
  • Tom, if you don't mind and know the answer, I've started a SO question regarding getting Xcode 6 + iOS 8 iCloud Core Data syncing: http://stackoverflow.com/questions/25971816/xcode-6-ios-8-icloud-core-data-setup I'm also looking for a iOS 8 iCloud Core Data solution along with a few others. – Zhang Oct 07 '14 at 09:05
  • 1
    Check out CKSIncrementalStore. https://github.com/CloudKitSpace/CKSIncrementalStore – Nofel Mahmood Jun 21 '15 at 23:01
  • Probably time to revise the post on CloudKit now that one year later they did exactly what you said you were not holding your breath for. – Rob Jul 21 '15 at 14:48
  • Which detail is that? There's still no REST API, though their JS stuff may help out there. – Tom Harrington Jul 21 '15 at 16:08
  • Is CKSIncrementalStore the only option for managing CloudKit with a Core Data app? – Z S Nov 24 '15 at 21:08
  • 3
    "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)." -- have you got a code sample/example on this comment? There are no solid examples that I can find :( – Ernest Jan 16 '16 at 22:00
  • @TurbZ try Apple's documentation? – Tom Harrington Jan 16 '16 at 22:30
  • 1
    @TomHarrington with that sort of feedback why even have stackoverflow if all we need to do is check documentation :( – Ernest Jan 16 '16 at 23:35
  • @TurbZ asking for sample code is not a good SO question. If you have an actual question, post it. – Tom Harrington Jan 17 '16 at 00:06
  • hi @TomHarrington do you know if there is another way to write the public database as an administrator, to seed and maintain data, without requiring an icloud login? full question: http://stackoverflow.com/questions/36808601/seed-and-maintain-cloudkit-public-database-still-requires-icloud-login – Crashalot Apr 23 '16 at 08:51
  • @TomHarrington in your update, you mention that all Core Data with iCloud API's have been deprecated, and should therefore be avoided. Is there any (very rough) estimate you would give as to when we might see its replacement? – Loic Verrall Jul 05 '16 at 02:02
  • @LoicVerrall I think CloudKit is as close as Apple is going to get to a replacement. It's definitely not not the same thing, but it's the only thing Apple is offering. If there's a direct replacement, Apple has kept it completely secret. – Tom Harrington Jul 05 '16 at 04:38
  • @TomHarrington thanks for your reply. I'm just about to start a new project (in fact, my university final year project) and I don't think I can wait in the case that Apple *might* release a "direct replacement" as you put it. I'm going to go ahead and use CloudKit and CoreData for now. If Apple do release a replacement, I'll just update my code. Thanks once again :) – Loic Verrall Jul 05 '16 at 12:19
  • @TomHarrington, I am developing an app that stores data locally on iPhone and will be able to sync data with iPad, do you have any suggestions. Is it possible to sync CloudKit with core data. – Coder221 Oct 02 '16 at 07:21
  • >>> 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. <<< So how is one supposed to do it now? – bcye Nov 13 '18 at 03:50
  • 1
    One isn’t supposed to do it now, not like this anyway. Core Data does not have any built in syncing mechanism. Apple provides CloidKit but there is no built in CloudKit to Core Data system. – Tom Harrington Nov 13 '18 at 04:33
27

With iOS 13, Apple announced new features in Core Data to better work with CloudKit. The main addition is NSPersistentCloudKitContainer which basically manages syncing between Core Data and CloudKit for you.

You can learn more in the WWDC session Using Core Data with CloudKit.

Apple also released a nice collection of docs for this very usage: Mirroring a Core Data store with CloudKit.

Clément Cardonnel
  • 4,232
  • 3
  • 29
  • 36
  • I just want to mention here that I'm hearing about many developers who are unsatisfied with CloudKit these times. There are many unreliability problems that directly impact their business and customers, and Apple is slow to fix them. I'd advise anyone interested in this tech stack to investigate these issues and take them into account in their decision before going any further. – Clément Cardonnel Mar 23 '22 at 08:22