My App is crashing trying to insert into a ModelContext. Anything obvious that sticks out as to why?
@Model
class MyDataClass {
var number : String
var pageNumberInDocument : Int
init(number: String, pageNumberInDocument: Int) {
self.number = number
self.pageNumberInDocument = pageNumberInDocument
}
}
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}.modelContainer(for:[MyDataClass.self])
}
}
struct SaveMyData {
@Environment(\.modelContext) var context
func saveData(number: String, pageNumberInDocument : Int) {
let dataToSave = MyDataClass(number: number, pageNumberInDocument: Int(pageNumberInDocument)!)
context.insert(dataToSave)
}
}
All seems pretty basic and correct to me. My crash ....
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Cannot insert 'MyDataClass' in this managed object context because it is not found in the associated managed object model.'
I have seen the same issue on the Apple dev forums but no significant reply to that thread. Hence my post. Really appreciate anyone able to even just state the obvious to me. Thank SO
func saveData(number: String, pageNumberInDocument : Int, context: ModelContext)
and pass the context from the view – Dative