Search code examples
swiftcore-datacloudkit

How to specify on which Core Data Configuration to create a new record using CloudKit


Let's suppose I have an app using CloudKit + Core Data sync.

My use case is:

  • provide the same read only data to all my users (a kind of baseline data that can be modified only by the admin)
  • each user will be able to write it's private data.

For doing that:

My entities are: "Entity1" and "Entity2".

My Configurations are: "Config1" and "Config2". Both have entity1 and entity2 and both are "usedWithCloudKit".

In my coreDataStack, I have 2 descriptions. The first one is for Config1 and the second one is for Config2 and has "databaseScope = .public"

By doing this, if the user saves something into the app, the record will appear in the private db.

If I manually add something to the public DB via iCloud Dashboard, it will be fetched by the user.

So far so good.

Now, if I'm an admin, how can I specify that my SAVE will go to the public DB and not the private one?


Solution

  • I managed to do this via:

    assign(_ object: Any, to store: NSPersistentStore)