SwiftUI Bug: Clear Navigation Stack when changing to any Tab

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.

The snippet you provided doesn't compile. Could you provide the code for SettingsView and the steps to reproduce the issue.

Have you also included some logs to debug the issue?

Sure!

Here I have the file with the complete code and an example video of the issue: https://github.com/era2099/navigationStackIssue/blob/main/ContentView.swift

Steps:

  • Run the app.
  • Select Settings Tab
  • Select one of the buttons on the list to navigate to another view
  • Rapidly select Home Tab (when navigation transition is happening)
  • Return to Settings Tab

Expected: Settings Tab should have the NavigationStack clean. Displaying the Main Settings View with the list of three buttons.

Actual: Settings Tab is still displaying the NavigationStack with one view inside.

SwiftUI Bug: Clear Navigation Stack when changing to any Tab
 
 
Q