I have a simple navigation router:
@Observable class BaseRouter {
var path = NavigationPath()
var isEmpty: Bool {
return path.isEmpty
}
func navigateBack() {
path.removeLast()
}
func popToRoot() {
path.removeLast(path.count)
}
}
And a subclass:
class ProfileRouter: BaseRouter {
enum ProfileViewDestination {
case viewA
case viewB
}
enum ViewADestination: FormNavigationItem {
case viewC
}
func navigate(to destination: ProfileViewDestination) {
path.append(destination)
}
func navigate(to destination: ViewADestination) {
path.append(destination)
}
}
When using navigate(to:) and navigateBack functions all works fine, but when when using NavigationLink and native navigation back button I see my path is not really updating, although the NavigationStack is navigating currently.
So when going back from navigation button I can see my path still containing the appended item, and when I'll navigate to the screen again it will present it twice and so on.
One solution for this issue is to override the native back button but not only it will cause a loss of native functionality (back stack, swipe back) it adds a lot of redundant boilerplate code, so it is a bad solution IMO.
As for NavigationLink I'm having the opposite issue, where it does not appending the proper item to the path and navigateBack will go 2 levels back.
I'm sure I am missing something stupid here, or that's just not the way 'NavigationPath' meant to be used. either way I can't seems to find any good example for this one.
Ideas?!
Use
ObservableObjectto make views update on changes of your classBaseRouter.@Observableonly update properties that are in the View's body.Apple Documentation "If body doesn’t read any properties of an observable data model object, the view doesn’t track any dependencies."
Because NavigationStack's path is not read in the body of its view,
@Observabledoesn’t track any dependencies.NavigationStack is:
Just add
ObservableObjectLike This:Note: You will then have to use
@StateObjectto initialize your classBaseRouter.Add
@EnviromentObjectto views that need to use the router:Alternate options to BaseRouter class methods:
Where Routeable is:
Then
enumconforming toprotocolRouteable:All that's left now is switching on YourAppsRoutes:
@ViewBuilder might be needed. You typically use ViewBuilder as a parameter attribute for child view-producing closure parameters, allowing those closures to provide multiple child views.
Hope this helps,
Xcode Version 15.1 (15C65)
Deployment: iOS 17.2
Apple Feedbacks link
FeedBack#: FB12969309