Getting Started with Core Data and CloudKit | raywenderlich.com

In this tutorial, you’ll learn how to use NSPersistentCloudKitContainer to integrate Core Data and CloudKit.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/13219461-getting-started-with-core-data-and-cloudkit

Hi, nice tutorial! I have question. If I use cloudkit backed CD in an app, can I access the cloudkit container in another app?

Hello. I hope you liked the tutorial :]
CloudKit containers can be shared between applications of the same vendor on the App Store. There’s a catch though; when you use Core Data with CloudKit, Core Data handles creating the CKRecords in CloudKit. You have to take care of not messing up data since another app may depend on it.

Thank you very much for the great tutorial!

The tutorial mentions that one can not use the same container for the NSPersistentCloudKitContainer and for other CloudKit-related code and data in the App, as otherwise some CloudKit related confusion happens. Hence, I wonder the following:

  • If I use iCloud Documents for some data and NSPersistentCloudKitContainer for some other (unrelated) data in the SAME App, can I then use the SAME container for this, or would I need 2 containers, one for my iCloud Documents, one for the NSPersistentCloudKitContainer?
    It is my understanding that it is ok to have an App that uses both iCloudDocuments and Cloudkit in the same APP (so there would be two check marks in “Signing&Capabilities”) and they use the same container. Is that right?

Another question, related to the modernizing-stack API, NSPersistentContainer, which underlies NSPersistentCloudKitContainer:

  • Regards the .viewContext, I wonder, what if I want to have 2 or more managed object contexts related all to the MAIN queue. I would want this for convenience since it is easier to separate the data that I want to show in the UI if I fetch one data type into one main managed object context and another into another. Is that possible with the modernised container API? Or would I have to go with the old stack API for achieving this? Or is that not possible at all?

Thank you very much for your insights!

Hello. I’m glad you liked the article.

• I would highly advise against using the same container for the use cases you mentioned. Messing the Core Data internal structure is very easy and it makes the app show unpredictable behaviors.

• You can use multiple persistent stores to segregate your data. Please take a look at this video from WWDC around the hour mark 14:40.

Hi, thank you very much for your answers.

One remaining part regards to question 1:

It is my understanding that it is ok to have an App that uses both iCloudDocuments and Cloudkit in the same APP (so there would be two check marks in “Signing&Capabilities”) and they use the SAME container. Here, I use NO Core Data or NSPersistentCloudKitContainer at all, but I use CloudKit API directly. Is that ok, or would even here 2 containers be needed? I ask since I never have seen any documentation / warning that one should not do this, but maybe you might have come across some…

Thank you very much!

To be honest, I’ve never come across the situation you described but I don’t know any reasons that it shouldn’t work. I believe you can safely use a single container for both iCloud Documents and CloudKit.

Thank you very much for your answer, I have similar feeling.

Thank you for the nice tutorial!

I’ve noticed that if I go to Settings > iCloud > Disable it for the app (PlaceTag), whenever I open my app again all the data will be gone.

Is this expected? Any way around it?

In this case I would like the local data to be displayed.

Cheers,

Hi,

That’s predictable. If you want to support local-only data storage, you need to configure your persistent container with appropriate options, or use a simple NSPersistentContainer altogether.

There are lots of interesting points regarding this issue in this Apple’s support document.

I came across this article and I believe it’d help too.