I have been developing iOS apps for 7 – 8 years now. For the first 6 years of my iOS career, the iOS App development has always been using MVC (Model, View, Controller). MVC is the default software architectural paradigm recommended by Apple. In the past, I have developed some complex iOS projects like Nestia. The View Controllers associated with the screens with complex UI will have thousands lines of code. Eventually MVC will become “Massive View Controller”. The View Controllers will become hard to manage and change. Especially when the same view controller is developed by more than 1 iOS Developer.
When I joined a new company about 1.5 years ago, I have decided to try out MVVM. MVVM (Model, View, View Modem) is a new software architectural paradigm. It can be used to replace MVC to have cleaner codes and avoid “Massive View Controller” from happening.
Here, I will share a quick comparison for MVC versus MVVM in iOS Apps Development.
MVC (Model, View, Controller)
Inside a MVC project, normally the codes have been distributed into a few layers:-
Model: Contain structure of data for local storage, show on UIs, parameters on function or API requests.
View: Storyboards, xib and associated view files such as UITableViewCell.
Controller: In iOS, we only have View Controller. From the name itself, it shows that it is both view and controller. So, there is no clear distinguish between view and controller inside a View Controller. Sometimes, View Controller may contains some View related codes due to various reasons.
Utilities: Contain small classes/structs/enums for constant declarations, utility/helper functions, settings/configurations.
Extension: Extend the capability of View and View Controllers to synch the consistency and avoid codes repetition.
Service: Contain codes related to data storage or API requests.
In MVC, normally View Controller will access the service layer directly. So, it will call API and after receiving data from the callbacks, it will manipulate the data and construct a model before showing it via a View. In some of the processes, View Controller might need some help from Utility and Extension. As you can see, View Controller really have too much responsibilities.
MVVM (Model, View, View Model)
Model, Utility, Extension, Service, View: They are basically either stay the same or with minor changes.
To reduce the responsibility of View Controller, View Model layer is created.
View Controller: It will only interact with View Model and will not interact with Model and Service directly. It main function is to present data from View Model to show on UI or to get data from from UI and present to View Model.
View Model: Contains codes related to calling a Service, data manipulation and construct model.
On top of MVVM, you may want to further enhance the codes on Service, View and View Controllers using RxSwift. It will make Reactive Programming possible in iOS projects. Reactive Programming is also a new programming paradigm which is associated with data streams and data changes. Instead of using API callbacks to get data, a particular view/view controller can subscribe to the potential data changes before reflecting the latest UI.
A Simple Todo iOS App using MVC and MVVM
I have developed a simple todo project to compare between MVC vs MVVM. The app contains a few basic features:-
– Sign Up
– Add Todo Item
– Complete Todo Item
– Show Todo List
– Add Name on Profile
For backend implementation, I am using Firebase Authentication (Login, Sign Up, Logout) and Cloud FireStore (Add Todo Item, Delete Todo Item).
I have uploaded the project to GitHub. There are 3 branches (Master and MVC):-
MVC Branch: It is implemented using MVC software architectural paradigm.
Master Branch: It is converted from MVC to MVVM software architectural paradigm with the help of RxSwift.
MVVM-Coordinator: It is MVVM + Coordinator pattern. Coordinator is basically a few classes that help in coordinating the app navigation. Coordinator also carries the responsibility to instantiate and inject dependencies for View Controller and View Model.
UnitTesting: The codes from MVVM-Coordinator branch has been refactored so that unit testing can be conducted easier. ViewModel is the main targeted test unit. There is a still room for improvement on the testings.
I hope that you find this post and the Github project helpful. 🙂