SwiftData Configurations for Private and Public CloudKit

I did manage to save my Entities to CloudKit with SwiftData but the default database is the private database. I need to store some Entities in the private and other Entities in the public CloudKit database. How do I manage that with SwiftData? With CoreData I always used different configurations for both private and public and added the entities to one or the other.

Answered by particleman in 755417022

ModelConfiguration so far lacks an NSPersistentCloudKitContainerOptions.databaseScope equivalent. Even if you were to divide data into two stores, it seems that the automatic CloudKit sync would still default to .private for both.

There is some discussion of coexistence between Core Data and SwiftData, with both accessing the same store. It might be possible to setup the Core Data side to perform the public database sync and the SwiftData side to operate as a reader of that store.

Maybe using several ModelConfiguration objects and specifying a CloudKit container identifier for those:

https://developer.apple.com/documentation/swiftdata/modelconfiguration/init(_:schema:url:readonly:cloudkitcontaineridentifier:)

So is the suggestion to initialize one CloudKit container as punlic and initialize another CloudKit container as private and then manage the data‘s destination accordingly? I mean, I guess this sounds ok. But is it a good approach? What might be some of the pitfalls?

Accepted Answer

ModelConfiguration so far lacks an NSPersistentCloudKitContainerOptions.databaseScope equivalent. Even if you were to divide data into two stores, it seems that the automatic CloudKit sync would still default to .private for both.

There is some discussion of coexistence between Core Data and SwiftData, with both accessing the same store. It might be possible to setup the Core Data side to perform the public database sync and the SwiftData side to operate as a reader of that store.

Can someone from Apple say whether or not SwiftData will support private, public, and shared databases when it goes live with iOS 17? Or any information regarding when it will have support in the future?

I don't see the value in migrating "some" of my CoreData code to SwiftData as it just means I have two different APIs to worry about, and others on my team will have to learn both in order to support persistence.

Thanks.

Hi all. Any updates to this thread or new information anyone can add.

SwiftData Configurations for Private and Public CloudKit
 
 
Q