What is MVVM?

Q) What is MVC?

Ans: It’s a design pattern and a standard approach to build iOS apps.


 In this design pattern, the controller separates the model from the view so that each can change independently. If we didn’t have such a decoupling then every time the model changed, the view would need to change as well.
However, as Apps get larger, then we should move off MVC, as it eventually becomes harder to test.

MVVM to the rescue

Lets look at this alternative pattern, MVVM. It stands for Model-View-ViewModel and it comes from Microsoft. It is quite similar to MVC, formalizes the tightly coupled nature of the View and Controller and introduces a new component.


Wait-.. isn’t this MVC but reworded? Nope. There is a difference. The views still don’t hold references to Model, but neither do Controllers. Instead, there is a new component called ViewModel.

             The ViewModel encapsulates data/properties that the view can bind to and any validation logic and actions that can be performed. Typically, the ViewModel interacts with the model by invoking methods in the model classes. The ViewModel then provides data from the model in a form that the view can easily use. In this pattern we are essentially taking the state of our app and putting it into a ViewModel.

                For the ViewModel to participate in two-way data binding with View, its properties must raise a PropertyDidChange notification, so that the View gets notified and can change its objects accordingly.


Benefits of MVVM

  • The ViewModel is an excellent place to put your validation logic for user input, view presentation logic, network requests. As a result, the ViewController files become far less bloated.
  • Also, the View-ViewController component is completely isolated from the model. So during development, one developer can work on the UI part of the screen and another can take care of the logic for the screen, independently and concurrently.
  • It is easy to redesign the UI without messing with the model logic since they are both completely isolated. As long as you link appropriate properties to the ViewModel, you can swap view components in and out. This gives more freedom to experiment with the UI.
  • For Universal apps, both iPad and iPhone ViewControllers can interact with the same ViewModel.
  • It’s easier to test. Developers can create unit tests for ViewModel and model without the View.
To bind the properties of the ViewModel to the ViewController, use KVO (Key-Value Observing).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s