The same thing happens to me as well, but only in the last xcode beta version (v14 beta 5).
I have a very veeery simple use case:
NavigationStack(path: $router.homeNavPath) {
Form {
Section {
ForEach(Item.dummyItems) { item in
NavigationLink(value: item) {
LabeledContent(item.name, value: item.price, format: .number)
}
}
}
}
.navigationDestination(for: Route.self) { route in
switch route {
case .testHomeDetailView(let item):
DummyViewHere()
}
}
}
final class Router: ObservableObject {
@Published var homeNavPath = NavigationPath()
}
The above view is literally everything i have inside the HomeView's body, nothing more.
Inside the main app struct I have this:
@main
struct Test_App: App {
@StateObject private var router = Router()
var body: some Scene {
WindowGroup {
HomeView()
.environmentObject(router)
}
}
}
If I tap on any item from that list it navigates to the dummy detail view but after not a second it pops back the screen to the home view.... this didn't happen in the previous versions of xcode...
Furthermore if I add a second section to the form like so:
Section {
VStack {
Text("Home view")
Button("navigate to test detail screen") {
router.homeNavPath.append(Item.dummyItems[0])
}
}
}
Now if I tap the second section's button it behaves like before (it's going to the detail screen and gets almost instantly popped back), but as an extra I also get this warning:
Publishing changes from within view updates is not allowed, this will cause undefined behavior
It doesn't matter if you replaced Form and Section with just a plain ScrollView or not, or if you replace the ForEach with a List, the behaviour is gonna be the same.
Furthermore if I use NavigationSplitView and i have a NavigationStack in the detail callback with the same path of router.path i get these warnings now:
Test[56762:693175] [UILog] Called -[UIContextMenuInteraction updateVisibleMenuWithBlock:] while no context menu is visible. This won't do anything.
Update NavigationRequestObserver tried to update multiple times per frame.
Update NavigationAuthority bound path tried to update multiple times per frame.
Now for the "Update NavigationRequestObserver" warning I haven't found anything yet anywhere which is kinda scary in a way.
I'm pretty sure it's some sort of a bug in this beta version, but if it's not, then the new navigation api has got some serious issues.
I really hope it's gonna be ok and fixed asap.
EDIT:
I found out in the end that the reason why it's navigating to the detail screen, but it's popping back in not even a second after, is because inside the DummyViewHere() (the detail view basically) you must NOT have a NavigationStack, but just build the content that would go in the nav view. I added the navigation stack by mistake in the detail view, even though it was just one place where i did this mistake. Lack of attention...
Still the other issues persist.