Post

Replies

Boosts

Views

Activity

Reply to SwiftUI Preview fail with Core Data: "Error Domain=NSCocoaErrorDomain Code=516"
Thanks for the help. I managed to see the line where the app crashed looking at the DiagnoticReport. The bug seems to be that the preview was using the App class, where the DataStore (singletons holding the core data container/context) was initiated normally, while in preview it is initiated for preview (as in the sample code provided with Persistence singleton): @main struct MyApp: App {     let appData = AppData.shared     let datastore = DataStore.shared          var body: some Scene {         WindowGroup {             ContentView()                 .environmentObject(appData)                 .environment(\.managedObjectContext, datastore.container.viewContext)         }     } } Changing it for that worked: @main struct MyApp: App {     let appData = AppData.shared     let datastore = DataStore.preview          var body: some Scene {         WindowGroup {             ContentView()                 .environmentObject(appData)                 .environment(\.managedObjectContext, datastore.container.viewContext)         }     } } I think I will gave to put a closure , with a check to verify if I'm in preview or not to initiate shared or preview... I don't understand why there no such need in the Xcode sample swiftui-with-core-data project...
Feb ’21