SwiftData struggles. Cannot insert 'MyClass' in this managed object context because it is not found in the associated managed object model
Asked Answered
T

2

5

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

Tribade answered 25/10, 2023 at 13:33 Comment(4)
The context probably isn’t available on init. Most SwiftUI wrappers aren’t but also SwiftUI wrappers are only available in Views, a plain struct doesn’t have a reason to have access to SwiftUIOrdure
Since posting I moved the init call out. Placed into its own function to call it from a button tap but same error. Would update Q to reflect only on mobile currentlyTribade
Change the function signature to func saveData(number: String, pageNumberInDocument : Int, context: ModelContext) and pass the context from the viewDative
@JoakimDanielson well I no longer get the error but nothing being saved into the store. However that's a different issue to the question I posted. As the error has now gone as result of your suggestion and so my Q answered. Please post as Answer and I will mark correct. Thank youTribade
D
4

You can't use @Environment outside of a View so you need to inject the model context instead. The easiest way to do this is to pass it to the function

func saveData(number: String, pageNumberInDocument: Int, context: ModelContext) {
    let dataToSave = MyDataClass(number: number, pageNumberInDocument: Int(pageNumberInDocument)!)

    context.insert(dataToSave)
}

Since the function isn't accessing self in any way you could make it static if you want.

Dative answered 26/10, 2023 at 6:43 Comment(2)
Instead of passing it, could you use self.context.insert(dataToSave)? This seems to fail for me and I can’t really pass it along since it’s in a callback closure from a file loader.Blur
Yes you can inject it into your type and into a stored property.Dative
B
2

The solution that worked for me was to move the .modelContext modifier to my ContentView() instead of being on the WindowGroup(). That’s what got rid of the error for me.

Blur answered 26/11, 2023 at 4:1 Comment(1)
your suggestion solved the issue for me, I'll check around to understand why this behaviour happens, Thanks for your solution!Guileless

© 2022 - 2024 — McMap. All rights reserved.