Coordinator Pattern

The interviewer asks

“What is the Coordinator pattern? Why use it?”

How to answer

A coordinator is an object that owns navigation for a flow: it creates screens, injects their dependencies and decides what comes next, so view controllers never push or present each other. Screens report intent ("item selected", "checkout finished") through closures or a delegate, and the coordinator turns that into navigation. That removes coupling between screens, puts the flow in one readable place, and makes deep links and A/B-tested flows easy to route. In SwiftUI the same idea is usually an @Observable router that owns a NavigationPath (or a typed route array) and the views just append routes.

Key points

  • Coordinator creates view controllers and wires their callbacks; screens don't know their destinations
  • Child coordinators for sub-flows (onboarding, checkout), removed from the parent when finished
  • Coordinators are UI objects: @MainActor final class
  • Callbacks capture the coordinator weakly; the navigation controller owns the screens
  • SwiftUI: @MainActor @Observable final class Router with path, injected with .environment(router), destinations mapped in .navigationDestination(for:)

If they dig deeper

  • Deep links: parse the URL into a route enum, then ask the coordinator or router to build the stack (router.path = [.order(id)]). See deep-linking.
  • Typed array vs NavigationPath: [Route] is inspectable and testable; NavigationPath is type-erased and supports mixed value types (and Codable restoration via path.codable).
  • Who owns the root coordinator? The scene delegate (UIKit) or the App / root view as @State (SwiftUI). Each scene gets its own, because each window has its own navigation.
  • Back button: in UIKit, the coordinator must notice a pop it didn't trigger (UINavigationControllerDelegate) to release child coordinators.

Common pitfalls

  • Strong capture of the coordinator in screen callbacks, creating retain cycles
  • Child coordinators never removed after their flow finishes, leaking whole flows
  • A coordinator that also fetches data and holds business logic
  • A single global router singleton shared across scenes on iPad and Mac

How to use it

UIKit coordinator:

swift
@MainActor
protocol Coordinator: AnyObject {
    func start()
}

@MainActor
final class ShopCoordinator: Coordinator {
    private let navigationController: UINavigationController
    private let catalog: any CatalogService
    private var children: [any Coordinator] = []

    init(navigationController: UINavigationController, catalog: any CatalogService) {
        self.navigationController = navigationController
        self.catalog = catalog
    }

    func start() {
        let list = ProductListViewController(catalog: catalog)
        list.onSelect = { [weak self] product in self?.showDetail(product) }
        navigationController.setViewControllers([list], animated: false)
    }

    private func showDetail(_ product: Product) {
        let detail = ProductDetailViewController(product: product)
        detail.onBuy = { [weak self] in self?.startCheckout(product) }
        navigationController.pushViewController(detail, animated: true)
    }

    private func startCheckout(_ product: Product) { /* create child coordinator, append to children */ }
}

SwiftUI router:

swift
enum Route: Hashable { case product(Product.ID), checkout }

@MainActor
@Observable
final class Router {
    var path: [Route] = []
    func show(_ route: Route) { path.append(route) }
    func popToRoot() { path.removeAll() }
}

struct ShopRootView: View {
    @State private var router = Router()

    var body: some View {
        NavigationStack(path: $router.path) {
            ProductListView()
                .navigationDestination(for: Route.self) { route in
                    switch route {
                    case .product(let id): ProductDetailView(productID: id)
                    case .checkout: CheckoutView()
                    }
                }
        }
        .environment(router)            // children call router.show(.checkout)
    }
}

Best practices

  • Keep screens ignorant of destinations; they emit intents
  • Mark coordinators and routers @MainActor; capture them weak in callbacks
  • Use child coordinators for sub-flows and release them when done
  • Model routes as a Hashable enum so deep links and tests can build stacks
  • Own one router or coordinator per scene

Related concepts

  • Navigation architecture, NavigationStack, deep linking, MVVM, delegate pattern, dependency injection