Anyone successfully able to add two or more ModelConfigurations to a ModelContainer?
DESCRIPTION OF PROBLEM:
When you create two ModelConfigurations for two different models and combine them into one ModelContainer, it seems the @Query fails to find the models. Crashes app with error:
“Thread 1: "NSFetchRequest could not locate an NSEntityDescription for entity name 'NumberModel'"
STEPS TO REPRODUCE
1 - Create a new iOS project.
2 - In ContentView.swift add this code:
import SwiftData import SwiftUI struct ContentView: View { @Query private var colors: [ColorModel] @Query private var numbers: [NumberModel] var body: some View { List { ForEach(colors) { color in Text(color.name) } ForEach(numbers) { number in Text(number.name) } } } } #Preview { ContentView() .modelContainer(for: [ColorModel.self, NumberModel.self]) }
3 - In App file, add this code:
import SwiftData import SwiftUI @Model class ColorModel { var name: String = "" init(name: String) { self.name = name } } @Model class NumberModel { var name: String = "" init(name: String) { self.name = name } } @main struct MultipleModelConfigsApp: App { private var container: ModelContainer init() { do { let config1 = ModelConfiguration(for: ColorModel.self) let config2 = ModelConfiguration(for: NumberModel.self) let container = try ModelContainer( for: ColorModel.self, NumberModel.self, configurations: config1, config2 ) self.container = container } catch { fatalError("ModelContainer creation failed.") } } var body: some Scene { WindowGroup { ContentView() .modelContainer(container) } } }
4 - Now run the app and observe the crash and the error stated above.
VERSION OF XCODE
Version 15.1 (15C65)
FEEDBACK REPORT
FB: FB13504577 (Xcode project attached to FB)