NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

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

   

In the SceneDelegate

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

var window: UIWindow?
var settings = UserSettings()

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    let contentView = ContentView().environmentObject(settings) // <- add here

    if let windowScene = scene as? UIWindowScene {
        let window = UIWindow(windowScene: windowScene)
        window.rootViewController = UIHostingController(rootView: contentView) // <- remove from here
        self.window = window
        window.makeKeyAndVisible()
    }
}

   

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)
    }
}

   

Did you try to use @StateObject?

@StateObject private var userSettings = UserSettings()

Then pass it as an environmentObject:

contentView.environmentObject(userSettings)

Note: @StateObject is only available in iOS 14 (SwiftUI 2.0).

You can find more info here.

If that didn't work, just try to add self when you pass it:

contentView.environmentObject(self.settings)

   

We are using SwiftUI "2.0".

Firebase reported a crash with our app: 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.

The user was on iOS 14.01, but I have not been able to reproduce the issue on iOS 14.1 or 14.2. I have not tried 14.01 yet. However as I have seen this error reported in other forums and it seems to be a bug with SwiftUI when environmentObject is not used with a view created by NavigationLink. The recommend solution was just to use environmentObject, but that seems to defeat the purpose of using the environment to store objects in the first place.

Does anyone know of any offical Apple bug fixes where this is addressed? If it's been fixed, then I won't worry about it in older iOS releases.

Thanks.

   

Hacking with Swift is sponsored by Essential Developer

SPONSORED From January 26th to 31st you can join a FREE crash course for iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a senior developer!

Save your spot now

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

You are not logged in

Log in or create account
 

Link copied to your pasteboard.