How to use the Coordinator pattern in iOS
Video Statistics and Information
Channel: Paul Hudson
Views: 43,346
Rating: 4.942102 out of 5
Keywords: swift, coordinator, coordinators, pattern, ios, mvc, app, Xcode, design
Id: 7HgbcTqxoN4
Channel Id: undefined
Length: 18min 11sec (1091 seconds)
Published: Fri Jan 25 2019
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.
The trick here is properly defining the scope of the functionality of the view controllers that you use. Otherwise, for a flow of 4-5 screens, the size of your coordinators blow up as it micro-manages the controllers in the flow.
I’ve worked on a large application that used this pattern (almost exactly as he’s described it) and it’s amazing how easy it is to swap order of things, or add special case screens to the flow under certain circumstances.
As an alternative, I’d highly recommend reading and considering some of the points that Ben Sandofsky makes in his article, replacing “Coordinators” with simple View Controllers. https://sandofsky.com/blog/controller-hierarchies.html
Still, great thanks to Paul Hudson and Soroush Khanlou for introducing the Coordinator pattern and all of the talks/videos/literature and resulting discussion on the topic!
Took about 10 min to transform a small project over to use this. Love the organization of this, and it also pointed out some stuff I should probably refactor properly
You should credit the maker of this video /u/twostraws
Maybe it's because I'm a Mac developer and not an iOS developer but I'm not getting it.
What's the duplication that he claims to be removing here, exactly? I only see a little duplication being added: the Storyboard ID must always equal the class name. Also, I suppose, the protocol implementation essentially duplicates the protocol definition.
(Nit: why use a forced cast for the storyboard, and assume the ID was always set correctly, but not a force dereference in the controller? If for some reason the coordinator was released, all of the IBActions would silently become NOPs.)
The sample is a trivial app which would have been even simpler with ordinary segues. What's the case where you'd want the ability to easily re-wire the flow of your app? What would the "Buy" button ever do, if not present the BuyViewController? As a user, it seems to me that encouraging a simple top-to-bottom flow is a great benefit: apps are simple enough that I can understand them.
This strategy adds code and indirection, and makes the flow less visible. These have real costs, and while I'll grant that these are often worthwhile, I don't see what he's getting out of it. Is he just concerned that the controller was becoming too "massive", and he needed to move some of that code elsewhere? The compiler doesn't care, and it's easy to split a class across files, without having to completely restructure everything.
How does the coordinator make sure that it only instantiates a controller once? Does it check its child controllers to make sure it doesn’t exist in there?
Edit: All credits to /u/twostraws