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.
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?
• 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.
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…
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.
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.