SALE: Save 50% on all my books and bundles >>

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

}

   

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. :(

   

Hacking with Swift is sponsored by RevenueCat

SPONSORED Building in-app subscriptions are hard. RevenueCat makes it simple. With their open source SDKs, you can painlessly implement subscriptions for your app in hours, not months.

Explore the docs to learn more

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.

Cascable unleashes the power of your camera and unlocks powerful workflows for shooting, managing, and geotagging your photos.

 
Unknown user

Not logged in

Log in