Discover the Coordinator Pattern
You’re more of a video kind of person? I’ve got you covered! Here’s a video with the same content than this article 🍿
You’ve heard of the Coordinator Pattern, but you’re not entirely sure how it works? 🤨
Don’t worry! I’ve got you covered! 😌
In just a few paragraphs we’ll go over everything you need to know to understand how this pattern works!
Let’s start with this ViewController:
When a user selects a movie in a list of movies, this ViewController is responsible for navigating to the details of that movie 📲
But this approach doesn’t scale very well 😔
For instance, it would be difficult to implement a deep link that programmatically navigates to the details of a movie...
And that’s why it makes sense to extract the navigation logic to a new object called a Coordinator!
So let’s do it in 5 easy steps 🔥
Step 1️⃣, we create the Coordinator:
Step 2️⃣, we extract the navigation logic to the Coordinator:
Step 3️⃣, the ViewController stores a weak reference to the Coordinator:
Step 4️⃣, we use this reference to inject the Coordinator into the ViewController:
Step 5️⃣, the ViewController can now rely on the Coordinator to perform the navigation logic:
And that’s it: we’ve successfully implemented the Coordinator Pattern 🥳
Now other parts of our app can also rely on the Coordinator: for instance our AppDelegate could trigger a navigation in response to receiving a push notification 👌
Here’s the code if you want to experiment with it!