I am struggling with an unwanted navigation that is triggered by iOS 15 SwiftUI machinery when an unrelated property of my data model changes. I've distilled this down to the following minimal app (I deliberately squeezed everything into a single view for the sake of brevity here.) To view the bug launch the app in iOS15 and navigate to the third view - then every two seconds it will navigate back and forth from that view. Any idea how to work around this bug?
import SwiftUI
class Model: ObservableObject {
static let singleton = Model()
private init() {
Timer.scheduledTimer(withTimeInterval: 2, repeats: true) { [self] _ in
unrelated += 1
}
}
@Published var unrelated: Int = 0
@Published var text = "Hello World"
@Published var showChild = false
@Published var showSubChild = false
}
struct ContentView: View {
@ObservedObject var model = Model.singleton
@State var checked = true
var body: some View {
NavigationView {
VStack {
Text(model.text)
Toggle("", isOn: $checked)
if checked {
NavigationLink("First View", destination:
NavigationLink("Second View", destination:
Text("Third View"),
isActive: $model.showSubChild
),
isActive: $model.showChild
)
} else {
Text("something else")
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View { ContentView() }
}
@main
struct MinTestApp: App {
var body: some Scene {
WindowGroup { ContentView() }
}
}