I have following SwiftUI code, which makes a connection (to the server) when the user navigates to this page (this is a part of navigation stack), and disconnects when the user navigate away (up).
This works fine as long as the user stays on this page, but breaks when the user clicks the "Child Page" link, because .onDisappear() event will be triggered when the user navigates down as well.
Is there any way to distinguish two different scenarios (navigate up and down)?
Code Block struct ServiceView: View { @ObservedObject var state:ServiceState var body: some View { ScrollView(.vertical) { VStack { ... NavigationLink( destination: ChildPage(state: state), label: { Text("Child Page") }) } } .onAppear() { state.connection.connect() } .onDisappear() { state.connection.disconnect() } } }
This works fine as long as the user stays on this page, but breaks when the user clicks the "Child Page" link, because .onDisappear() event will be triggered when the user navigates down as well.
Is there any way to distinguish two different scenarios (navigate up and down)?