I have a very simple test app that shows a behavior I simply do not understand. I think this is a SwiftUI bug (filed as FB11518877), but I'm posting here in the hopes that maybe someone will see it and actually give me some kind of feedback/info (because I rarely get any via the Feedback app, ironically).
Here's the entire code:
struct ContentView: View {
@State var data: [String] = []
@State var navPath = NavigationPath()
var body: some View {
NavigationStack(path: $navPath) {
Button("Hit It") {
print("click")
navPath.append(0)
}
.navigationDestination(for: Int.self) { index in
let _ = print("index: \(index) data: \(data.count)")
if data.indices.contains(index) {
Text(data[index])
} else {
Text("Not found.")
}
}
}
.task {
data = ["Item 1", "Item 2", "Item 3"]
}
}
}
And here's a screenshot showing what happens when I click the button:
If you look down in the Console area, you can see that it prints out "click" and then the print statements tucked into the navigationDestination closure are also seen. For some reason there are 3 printouts (which seems odd, too), but the main problem seems to be that the 3rd printout indicates that the data array has suddenly become empty and so it returns the Text("Not found.") view instead of the one I expected.
Does anyone have any idea what's going on here?
This behavior is actually breaking our app when running on iOS 16.1 beta, but oddly enough, when I was trying to narrow it down and came up with this simple example, it turns out this also behaves this way on iOS 16. So something has changed in 16.1 to make this somehow worse in the case of our app, but it looks to me like this bug (I assume) has been around probably since WWDC.