I trimmed things down to this very simple example (a vanilla SwiftUI project, edit only ContentView and one line in SceneDelegate). Sets up a trivial ObservableObject and a couple of Views for screens. With latest Xcode 11.4 and simulator crashes very easily.
import SwiftUI
// NB In SceneDelegate added: let contentView = ContentView().environmentObject(EO())
class EO: ObservableObject {
@Published var n = 2
}
struct ContentView: View {
@EnvironmentObject var eo: EO
var body: some View {
NavigationView {
VStack {
Text("A: \(eo.n)")
NavigationLink(destination: ContentViewB()) {
Text("Go to B")
}
}
}
}
}
struct ContentViewB: View {
@EnvironmentObject var eo: EO
var body: some View {
VStack {
Text("B: \(eo.n)")
}.onAppear {
self.eo.n += 1
}
}
}
Am I doing something wrong? Or is this a SwiftUI bug? It seems to work fine initially then on re-navigating to child view it crashes. Though the exact behaviour is non deterministic (might crash on 2nd or 3rd navigation!)