I'm trying to set up an application using SwiftData to have a number of models backed by a local datastore that's not synced to CloudKit, and another set of models that is. I was able to achieve this previously with Core Data using multiple NSPersistentStoreDescription
instances.
The set up code looks something like:
do {
let fullSchema = Schema([
UnsyncedModel.self,
SyncedModel.self,
])
let localSchema = Schema([UnsyncedModel.self])
let localConfig = ModelConfiguration(schema: localSchema, cloudKitDatabase: .none)
let remoteSchema = Schema([SyncedModel.self])
let remoteConfig = ModelConfiguration(schema: remoteSchema, cloudKitDatabase: .automatic)
container = try ModelContainer(for: fullSchema, configurations: localConfig, remoteConfig)
} catch {
fatalError("Failed to configure SwiftData container.")
}
However, it doesn't seem to work as expected. If I remove the synced/remote schema and configuration then everything works fine, but the moment I add in the remote schema and configuration I get various different application crashes. Some examples below:
A Core Data error occurred." UserInfo={Reason=Entity named:... not found for relationship named:...,
Fatal error: Failed to identify a store that can hold instances of SwiftData._KKMDBackingData<...>
Has anyone ever been able to get a similar setup to work using SwiftData?
Thanks for providing the project. The issue happens because you don't specify a name for your model configurations.
When using multiple configurations, give each configuration a unique name so SwiftData knows how to separate the data and schema.
The following code fixes the issue:
let fullSchema = Schema([
LocalModel.self,
RemoteModel.self,
])
let localSchema = Schema([LocalModel.self])
let localConfig = ModelConfiguration("Local", schema: localSchema, cloudKitDatabase: .none)
let remoteSchema = Schema([RemoteModel.self])
let remoteConfig = ModelConfiguration("Remote", schema: remoteSchema, cloudKitDatabase: .automatic)
container = try ModelContainer(for: fullSchema, configurations: localConfig, remoteConfig)
Best,
——
Ziqiao Chen
Worldwide Developer Relations.