Working with the Coordinator pattern
The NavigationPath
and NavigationStack
combination is robust and provides flexibility in managing navigation. However, as our app scales, controlling how the user moves from screen to screen becomes more complex.
For example, let’s say we have an onboarding flow and want a different set of screens for different user profiles. Or, we want to reuse the same screen within different flows. In each flow, the screen should continue to a different screen.
In each case, it becomes difficult to understand our next view when we are within the screen context. In fact, this problem of managing our navigation is not related only to SwiftUI, and most developers know that from UIKit.
To try and improve our navigation mechanism, we can use what’s called a Coordinator pattern – a pattern that delegates the navigation logic to a dedicated component.
Let’s try to understand what it means.