MVVM -C iOS Design Pattern

MVVM-C Layers

Model:

The Model is a representation of the data. It must be as plain as possible without business logic.

View:

The 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:

The ViewModel is the core layer of this architectural pattern. It has the responsibility to keep the View and 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.

Coordinator:

Its responsibility is to show a new view and to inject the dependencies which the View and ViewModel need.

Pros:

  • 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.

Cons:

  • 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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store