Reddit has been hard at work in the last year to improve the performance of their iOS app while also making it suitable for faster iteration cycles, improved test coverage, and better extensibility. All of this was made possible by evolving the app original MVC architecture into a Model-View-Presenter (MVP) architecture.
One of the major issues with the original MVC design is its proneness to massive view controllers, due to the tight coupling between the view and the controller that it favours. Among the effects of this was a noticeable degradation of scroll performance from 60fps to 45–55 fps, writes Reddit engineer Andrew Rohn. Any attempt to improve the view implementation required duplicating thousands of controller lines of code due to the view-controller coupling. So, Reddit engineers set out to rewrite parts of their app and move to a Model-View-Presenter (MVP) design.
While not too different from MVC, MVP enables a better separation of presentation and business logic, as the following picture shows.
Following this idea, Reddit engineers removed any non-view logic from the UIViewController
and moved it to a Presenter class solely responsible for the view-model interaction. Thus the UIViewController
can take on responsibilities such as dealing with user input, setting up the view, bindings, and managing the view lifecycle. The Presenter concerns itself with data fetching and transformation, and the presentation. A key point of MVP is that the Presenter has no direct access to the view
object and accesses it through an interface.
Moving to MVP improved the modularity and reusability of the code and made the business logic testable in isolation from the view. Among other benefits of MVP in comparison with MVC, Rohn highlights its better suitability for composition over inheritance, since the Presenter is a component separated from the view controller and can be more easily reused or swapped with a different implementation. Most importantly, MVP enabled the rewrite of the view layer implementation to solve the scroll performance issue by going multithreaded with Texture. The major hurdle using Texture, explains Rohn, was dealing with state access synchronization, which required locking the state while the Presenter is busy rendering the feed UI.
MVP is just one option to move beyond MVC among many. If you are interested in a comparative analysis of a number of alternative approaches to iOS app architecture, do not miss the book App Architecture, iOS Application Design Patterns in Swift by Chris Eidhof, Matt Gallagher, and Florian Kugler.