SwiftUI EnvironmentObject in watchOS 7 doesn't function properly compared to watchOS 6

Originator:josh.hrach
Number:rdar://FB8668112 Date Originated:10 Sept 2020
Status:Open Resolved:No
Product:SwiftUI Framework Product Version:watchOS 7
Classification:Incorrect Behavior Reproducible:Always
 
In watchOS 6, destination views that were navigated to in a SwiftUI app would properly inherit their parent environment.

In watchOS 7, they no longer do.

Below is the sample code that can be used to cause the issue. It is also included in the sample project attached to this Feedback.

--

struct ContentView: View {
    @ObservedObject var model = Model()
    
    var body: some View {
        ScrollView {
            Text("Model Value \(model.value)")
            
            NavigationLink(destination: DetailView()) {
                Text("Manage Model")
            }
        }
        .environmentObject(model)
    }
}


struct DetailView: View {
    @EnvironmentObject var model: Model
    
    var body: some View {
        Button("Increment") {
            self.model.value += 1
        }
    }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}


class Model: ObservableObject {
    @Published var value = 0
}


--

The above will show a NavigationLink that, when pressed, will show a detail view with a button.

In watchOS 6, the button works as expected and updates the environment object.

In watchOS 7, the app crashes when trying to access the environment object with the error: "Fatal error: No ObservableObject of type Model found. A View.environmentObject(_:) for Model may be missing as an ancestor of this view."

This is clearly a regression from how things worked in watchOS 6.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!