MVVM -C iOS Design Pattern
This will be just a definition of what MVVM -C Architectural Pattern is and their Advantages and Dis-advantages.
Nowadays, the biggest challenge for an iOS developer is the craft of a robust application which must be easy to maintain, test and scale. In this article you will learn a reliable approach to achieve it.
It is a combination of the Model-View-ViewModel architecture, plus the Coordinator pattern.
Model is a representation of the data. It must be as plain as possible without business logic.
View is a
UIKit object—like a common
UIViewController. It usually has a reference of the
ViewModel—which is injected by the
Coordinator—to create the bindings.
ViewModel is the core layer of this architectural pattern. It has the responsibility to keep the
Model updated. Since the business logic is inside this class, you should use different components with single responsibilities to keep the
ViewModel as clean as possible.
Its responsibility is to show a new view and to inject the dependencies which the
Coordinator must provide a
start method to create the MVVM layers and add
View in the view hierarchy.
- View doesn’t contain logic, it just configures itself based on ViewModel.
- ViewModel is UIKit independent and fully testable. Thanks to communication through RxSwift it doesn’t know about Coordinator nor about View.
- Views and ViewModels are reusable because they are indepdent and doesn’t contain knowledge about application’s flow.
- Coordinator is able to handle passing data between ViewModels.
- Each screen requires a lot of boilerplate. You need to create Coordinator, ViewController, ViewModel and bind all together.
- RxSwift is quite tricky if you are not careful enough. It’s easy to cause memory leak, that’s why you have to invest more time in debugging.
- Bindings are not supported natively (unlike in Xamarin.Forms), therefore it is required to write a lot of “binding code” each time even when using RxSwift.
- RxSwift may become hard in debugging once code complexity increases.
Will be adding some examples with proper explanation in next article.