Thanks for the answer 🙂
When using ObservableObject for such purposes, when passing a navigation path from it to NavigationStack, there is such a problem that there are many unnecessary calls to navigationDestination and unnecessary reinitialization of View from the entire path https://feedbackassistant.apple.com/feedback/14536210
Yeah, that's true. Though that only really happens when the navigation state changes, which doesn't happen too often (since the ObservableRouter is just a Router, not a view model holding lots of states), and most of the times, the paths aren't that deep for it to have a huge impact, I think. And with @Observable unnecessary redraws should occur even less.
Additionally, due to the presence of EnvironmentObject, most likely some operations occur that lead to a loop of this process.
Yep, I'd love to understand what that loop actually looks like. That's really tricky to find out, unfortunately.
Also, it is better to move navigationDestination as close to NavigationStack as possible, i.e. it is better to move it from MiddleView to ContentView. An example according to your code:
Is that the case? I've never heard that before 🤔Do you have a link where this is stated/explained? That would make me rethink some of the architectural decisions in my app. I assumed the entire purpose of that modifier is to place it anywhere in a NavigationStack, even on sub-pages.
The problem with moving everything to ContentView is that you lose all modularity. ContentView would be tightly coupled with MiddleView, which is what I really want to avoid.
But if you want to leave it the same, you can use the @State variable for the navigation path, which is synchronized with the path from the Navigator, this will also fix the problem for this particular case, and will reduce the number of unnecessary navigationDestination calls and reinitializations to zero (not on all OS versions), example:
I've seen this approach a few times. It does work, yeah, but I don't like it very much, to be honest. It kind of defeats the point of SwiftUI and its state mechanisms. This manual synchronisation is exactly what declarative code is trying to get rid of.