You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Playin' with SwiftUI and Core Data brought me into a curious problem. So the situation is the following:
I have a main view "AppView" and a sub view named "SubView". The SubView view will be opened from the AppView view if I click the plus button in the NavigationTitleBar as popover or sheet.
The SubView view has a little form with two TextField objects to add a forename and a surname. The inputs of this two objects are handled by two separate @State properties. The third object in this form is simple button, which should save a the fore- and surname to an attached Customer Entity for CoreData.
If I try to save the Customer entity this way, I get the error: "nilError", specially: "Unresolved error Error Domain=Foundation._GenericObjCError Code=0 "(null)", [:]" from NSError.
But after figuring out, that when I add `.environment(.managedObjectContext, context)` to the SubView() call like so
// Get the managed object context from the shared persistent container.letcontext = (UIApplication.shared.delegateas! AppDelegate).persistentContainer.viewContext// Create the SwiftUI view and set the context as the value for the managedObjectContext environment keyPath.// Add `@Environment(\.managedObjectContext)` in the views that will need the context.letcontentView = AppView().environment(\.managedObjectContext, context)
I think it's not the correct behaviour. I've creating a separate project to reproduce the problem fast.
The text was updated successfully, but these errors were encountered:
I really don't know if Core Data is supposed to pass the `.managedObjectContext` environment key through, but does this work if you pass it as an environment object to your AppView when you create it in your AppDelegate (or SceneDelegate) instead?
Attachment: Download
Additional Detail from JIRA
md5: dd24002fbfb63380688e58c3555c6ad9
Issue Description:
Playin' with SwiftUI and Core Data brought me into a curious problem. So the situation is the following:
I have a main view "AppView" and a sub view named "SubView". The SubView view will be opened from the AppView view if I click the plus button in the NavigationTitleBar as popover or sheet.
The SubView view has a little form with two TextField objects to add a forename and a surname. The inputs of this two objects are handled by two separate @State properties. The third object in this form is simple button, which should save a the fore- and surname to an attached Customer Entity for CoreData.
If I try to save the Customer entity this way, I get the error: "nilError", specially: "Unresolved error Error Domain=Foundation._GenericObjCError Code=0 "(null)", [:]" from NSError.
But after figuring out, that when I add `.environment(.managedObjectContext, context)` to the SubView() call like so
it's works like a charm.
I think it's not the correct behaviour. I've creating a separate project to reproduce the problem fast.
The text was updated successfully, but these errors were encountered: