Coordinators

Coordinator Pattern

The Coordinator Patter is a design pattern proposed by Soroush Khanlou at NSSpain 2015. To understand it, we highly recommend investing the time to watch the presentation video https://vimeo.com/144116310.

In summary, this pattern allow us to move some of the concerns a UIViewController has to a Coordinator class. And you can see in practice with Coordinate the many benefits this practice has.

Coordinators

Coordinate offers a set of Coordinators classes and Protocols. We outline the Coordinators classes in this section.

Coordinator Class

Inheritance: Coordinator

Does not manage children Coordinators.

This is the base Coordinator class, for cases that you do not need manage any child Coordinator. Intended to be used as a wrapper to your UIViewController and manage its data fetching and other concerns, keeping your UIViewController as simple as possible.

ContainerCoordinator Class

Inheritance: Coordinator -> ContainerCoordinator

Manages children Coordinators.

The ContainerCoordinator is the most basic Coordinator that can manage other children Coordinators. The idea is that you can use it as a "switch" between any kind of Coordinator.

NavigationCoordinator Class

Inheritance: Coordinator -> ContainerCoordinator -> NavigationCoordinator

Manages children Coordinators.

The NavigationCoordinator is a specialized ContainerCoordinator, to be used with a UINavigationController as rootViewController and has specific logic to cope with the intricacies of said UINavigationController. It has specialized presentation methods that mirror the ones from UINavigationController

TabBarCoordinator Class

Inheritance: Coordinator -> ContainerCoordinator -> TabBarCoordinator

Manages children Coordinators.

The TabBarCoordinator is a specialized ContainerCoordinator, to be used with a UITabBarCoordinator as rootViewController and has specific logic to cope with the intricacies of said UITabBarController. It has specialized presentation methods that mirror the ones from UITabBarController

We do not cover all the UIViewControllers. The idea is that you can use the ContainerCoordinator for most of your cases. If not, then you can subclass it and adapt to your needs. And if you think it is generic enough and can be shared, feel free to contribute to the project.

Structure

If you imagine the structure of an app as a tree with nodes ,branches and leafs, the Coordinators with children (ContainerCoordinator, NavigationCoordinator, TabBarCoordinator) are the nodes, the presentation methods are the branches and the Coordinators without children are the leafs (Coordinator)

To illustrate that, here is a simplified diagram of the navigation structure from the Example App

Coordinate Exampe App Navigation Diagram

Notice how the Coordinators "wrap" the UIViewControllers. This is so that the Coordinators can take care of the Navigation and just display the "wrapped" UIViewControllers. That means, you won't be calling presentation methods directly on your UIViewControllers, like func show(UIViewController, sender: Any?) or func pushViewController(UIViewController, animated: Bool), but instead you will use Events for that, like we show bellow in Navigation

Found errors? Think you can improve this documentation? Simply click the Edit link at the top of the page, and then the icon on Github to make your changes.