Hi, I have a project that uses SwiftUI, where we have a TabView, and in one of the tabs, I have a NavigationStack(path:). And created a logic to clear the NavigationStack path everytime you change to another Tab.
But found a bug, that if for some reason when doing a navigation in the NavigationStack, and rapidly tapping to another Tab, the NavigationStack doesn't gets clean up, or even if you have for some reason something allocated in the view you were navigation, doesn't get deinit if you have the logic for deinit a object when dismissing the view.
The environment I have is: iPhone 12 Pro Max with iOS 17.6.1
This is the code that I have:
struct TabBarView: View {
@ObservedObject var tabBarVC = TabBarViewController()
var body: some View {
TabView(selection: $tabBarVC.selectedTab) {
Text("HomeView")
.tabItem {
Label("Home", systemImage: "house")
}
.tag(TabBarViewController.Tab.home)
SettingsView(settingsVC: tabBarVC.settingsVC)
.tabItem {
Label("Settings", systemImage: "gear")
}
.tag(TabBarViewController.Tab.settings)
}
.onChange(of: tabBarVC.selectedTab) { oldValue, newValue in
tabBarVC.settingsVC.clearNavigationPath()
}
}
}
class TabBarViewController: ObservableObject {
enum Tab {
case home
case settings
}
@Published var selectedTab: Tab = .home
@Published var settingsVC: SettingsViewController = .init()
}
class SettingsViewController: ObservableObject {
enum Destination {
case viewOne
case viewTwo
case viewThree
}
@Published var navigationPath: NavigationPath = .init()
func navigateTo(destination: Destination) {
self.navigationPath.append(destination)
}
func clearNavigationPath() {
self.navigationPath = .init()
}
}
The expected I am looking for is that everytime you change your tab, it cleans up the navigation stack, even if you change the tab when there is a navigation animation in process.