Hi, I've been working on an app that - so far - has only had to deal with offline data store.
The usage is fairly simple: the user picks their favourite tv shows and networks, and they get persisted with SwiftData with all the correct relationships.
Now, I would like to migrate the local storage of the users to a private CloudKit db, but every time I upgrade the app, the data seems to disappear completely.
This is the snippet evolved through the attempt of migrating the data:
Current Production code
public static func makeModelContainer() -> ModelContainer {
do {
let processRequiresInMemory = ProcessInfo.processInfo.arguments.contains("inMemoryDatabasePreferred")
let modelConfiguration = ModelConfiguration(
isStoredInMemoryOnly: processRequiresInMemory,
groupContainer: .automatic,
cloudKitDatabase: .none
)
let modelContainer = try ModelContainer(
for: Country.self,
Episode.self,
Movie.self,
Season.self,
Show.self,
Network.self,
NetworkSubscription.self,
migrationPlan: AppModelMigrationPlan.self,
configurations: modelConfiguration
)
return modelContainer
} catch {
fatalError("Could not initialize model container")
}
}
Testing CloudKit enabled
public static func makeModelContainer() -> ModelContainer {
do {
let processRequiresInMemory = ProcessInfo.processInfo.arguments.contains("inMemoryDatabasePreferred")
let modelConfiguration = ModelConfiguration(
"synced",
isStoredInMemoryOnly: processRequiresInMemory,
groupContainer: .automatic,
cloudKitDatabase: .automatic
)
let modelContainer = try ModelContainer(
for: Country.self,
Episode.self,
Movie.self,
Season.self,
Show.self,
Network.self,
NetworkSubscription.self,
migrationPlan: AppModelMigrationPlan.self,
configurations: modelConfiguration
)
return modelContainer
} catch {
fatalError("Could not initialize model container")
}
}
}
The differences, which I don't understand fully because I could not find documentation, are:
ModelContainer(...) -> ModelContainer("synced", ...)
cloudKitDatabase, from none to automatic.
I have the feeling that changing the name of the configuration also changes some reference to the db itself, but if I were not to change the name, the app would have crashed because unable to migrate.
What's the best approach to take here?
4
1
1.5k