NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

Environment object not being inherited by child (sometimes) and app crashes

Forums > SwiftUI

I'm using the code from Paul's article "How to use @EnvironmentObject to share data between views". I just updated to xcode 11.4. This example crashes every time if you click "Increase Score" first followed by "Show Detail View". If I click "Show Detail View" first, it navigates to the view and I can navigate back. If I do that sequence 3, 4, or 5 times it crashes. The error is "Fatal error: No ObservableObject of type UserSettings found. A View.environmentObject(_:) for UserSettings may be missing as an ancestor of this view."

This example works as expected, however, in xcode 11.3. Anyone else seeing this issue? I noticed it first in my own project that stopped working and found this simple example from Paul as a testbed.

// SceneDelegate class UserSettings: ObservableObject { @Published var score = 0 }

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

var window: UIWindow?
var settings = UserSettings()

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    let contentView = ContentView()

    if let windowScene = scene as? UIWindowScene {
        let window = UIWindow(windowScene: windowScene)
        window.rootViewController = UIHostingController(rootView: contentView.environmentObject(settings))
        self.window = window
        window.makeKeyAndVisible()
    }
}

// ContentView struct ContentView: View { @EnvironmentObject var settings: UserSettings

var body: some View {
    NavigationView {
        VStack {
            // A button that writes to the environment settings
            Button(action: {
                self.settings.score += 1
            }) {
                Text("Increase Score")
            }

            NavigationLink(destination: DetailView()) {
                Text("Show Detail View")
            }
        }
    }
}

}

struct DetailView: View { @EnvironmentObject var settings: UserSettings

var body: some View {
    // A text view that reads from the environment settings
    Text("Score: \(settings.score)")
}

}

1      

Hi,

I've the same problem as well. Have you found something so far?

Thanks in advance.

   

Hi again,

After I found this Paul's tweet (https://twitter.com/twostraws/status/1146315336578469888?s=20) I tried to send environment objects to NavigationLink as well. Now it works. It may help you too. It seems it's a bug and I'm really tired on struggling SwiftUI bugs. :(

   

This has been bugging me as well. Everything runs fine in my simulator, but the SwiftUI preview does not work and it's frustrating as that is supposed to be one of the perks of SwiftUI.

I get this:

Fatal error: No ObservableObject of type Observer found. A View.environmentObject(_:) for Observer may be missing as an ancestor of this view.: file SwiftUI, line 0

Even though I've passing everything appropriately in the SceneDelegate and in each View Preview

   

I have a screenCoordinator class that is defined as an EnvironmentObject in SceneDelegate as follows:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?
    var screenCoordinator = ScreenCoordinator()

    //
    // general code
    //
    let contentView = HomeView(viewModel: viewModel)
                .environment(\.managedObjectContext, context)
                .environmentObject(self.screenCoordinator)

This was breaking a number of my View previews and I managed to fix it by instatiating the 'ScreenCoordinator' class in the preview function as follows:

struct AddItemView_Previews: PreviewProvider {
    @State static var screenCoordinator = ScreenCoordinator()

    static var previews: some View {
        AddItemView()
            .environmentObject(self.screenCoordinator)
    }
}

   

Subscribe to Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

Not logged in

Log in
 

Link copied to your pasteboard.