Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-11607] Passtrough of @Environment to Popover or Sheet View not working as expected #54015

Open
swift-ci opened this issue Oct 13, 2019 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-11607
Radar rdar://problem/56269978
Original Reporter nitaco (JIRA User)
Type Bug

Attachment: Download

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

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.

@Environment(\.managedObjectContext) var managedObjectContext
 @State private var modal: Bool = false
 ...
 Button(action: {
     self.modal.toggle()
 }) {
     Image(systemName: "plus")
 }.popover(isPresented: self.$modal){
     SubView()
 }

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.

...
@Environment(\.managedObjectContext) var managedObjectContext
 ...
Button(action: {
    let customerItem = Customer(context: self.managedObjectContext)
    customerItem.foreName = self.forename
    customerItem.surname = self.surname
 
    do {
        try self.managedObjectContext.save()
    } catch {
        print(error)
    }
    }) {
        Text("Speichern")
    }

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

SubView().environment(\.managedObjectContext, context)

it's works like a charm.

// Get the managed object context from the shared persistent container.
 let context = (UIApplication.shared.delegate as! 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.
 let contentView = AppView().environment(\.managedObjectContext, context)

I think it's not the correct behaviour. I've creating a separate project to reproduce the problem fast.

@belkadan
Copy link
Contributor

@DougGregor, @harlanhaskins, any insights? Is this a compiler bug, a SwiftUI bug, or unexpected-but-correct behavior?

@harlanhaskins
Copy link
Collaborator

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?

If so, that sounds like expected behavior.

@harlanhaskins
Copy link
Collaborator

Oh, I see, I looked in the project. Unfortunately, this does look to me like a bug in the SwiftUI environment passing system.

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

3 participants