Reputation: 21111
I have 2 models in my app, fist (ModelA) is working fine - data is persistent no matter how many times I restart app on the simulator, but the second (ModelB) sometimes losts it data and I see errors about no such table found in Xcode debugger. I don't understand what is the difference between them.
@Model
final class ModelA: Identifiable {
@Attribute(.unique) var id: String
var name: String
init(id: String = UUID().uuidString, name: String) {
self.id = id
self.name = name
}
}
@Model
final class ModelB: Identifiable {
@Attribute(.unique) var id: String
var param: String
init(id: String = UUID().uuidString, param: String) {
self.id = id
self.param = param
}
}
@main
struct MyApp: App {
var sharedModelContainer: ModelContainer = {
let schema = Schema([
ModelA.self,
ModelB.self
])
let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)
do {
return try ModelContainer(for: schema, configurations: [modelConfiguration])
} catch {
fatalError("Could not create ModelContainer: \(error)")
}
}()
var body: some Scene {
WindowGroup {
TabView { /* === views === */ }
}.modelContainer(sharedModelContainer)
}
upd I wasn't able to find the root cause and fix it (even though it is data fetch issue I believe it relates to how view hierarchy is structured), but I found workaround instead - I made second model a struct (Identifiable, Codable, Hashable)
Upvotes: 2
Views: 212
Reputation: 41
In my case, I was creating a modal container for my each repository, after creating a single container for all my repositories to fetch the models solved the problem.
Upvotes: 0