I have read lots of other questions and answers about infinite loops in SwiftUI. My question is different, although maybe this typo question is relevant, but I do not think so.
I have narrowed the problem to this: in a NavigationStack
, a lower level navigationDestination
that uses a different identifiable type in the destination closure than the for
data type, creates an infinite loop at the upper level navigationDestination
destination closure.
I have spent several hours reducing and abstracting the recreate code. This is as condensed as I could make it. When I simplify further, the infinite loop disappears, and I cannot determine why, yet. For example, I created a single layer NavigationStack
(not shown) where the destination closure does not use the for
data type, but it works correctly.
struct F3: Identifiable, Hashable {
let id: String = UUID().uuidString
let t: String
}
struct R3: Identifiable, Hashable {
let id: String = UUID().uuidString
let t:String
}
struct N3: Identifiable, Hashable {
let id:String = UUID().uuidString
let t: String
}
struct LV3: View { // Use `App` conformer to load this View in WindowGroup.
let f2z = [ F3(t: "A"), F3(t: "B"),]
var body: some View {
NavigationStack {
List(f2z) { f in
NavigationLink(f.t, value: f)
}
.navigationDestination(for: F3.self) { f in
VV3() // Infinite loop here.
}
.navigationTitle("L")
}
}
}
struct VV3: View {
let r = R3(t: "rrr")
let nz: [N3] = [
N3(t: "hhh"),
N3(t: "ttt"),
]
var body: some View {
List(nz) {
NavigationLink($0.t, value: $0)
}
.navigationDestination(for: N3.self) { n in
Text(r.t) // Changing to String literal or `n.t` fixes the infinite loop.
}
.navigationTitle("V")
}
}