MVVM

Presentation Logic

Things like transforming values from the model into something the view can present, like taking an NSDate and turning it into a formatted NSString.

We’re missing something from our diagram. Something where we can place all of that presentation logic. We’re going to call this the 'view model’ – it will sit between the view/controller and the model:

This diagram accurately describes what MVVM is: an augmented version of MVC where we formally connect the view and controller, and move presentation logic out of the controller and into a new object, the view model. MVVM sounds complicated, but it’s essentially a dressed-up version of the MVC architecture that you’re already familiar with. It’s easy to see how it can be incorporated into an existing app with a typical MVC architecture

Why using MVVM

The motivation behind MVVM on iOS, for me, anyway, is that it reduces the complexity of one’s view controllers and makes one’s presentation logic easier to test.

  • MVVM is compatible with your existing MVC architecture.
  • MVVM makes your apps more testable.
  • MVVM works best with a binding mechanism/framework.

Binding System

  • For mutable models, we’d need to use some kind of binding mechanism so that the view model can update its properties when the model backing those properties chang
  • Furthermore, once the models on the view model change, the views’ properties need to be updated as well.
  • A change from the model should cascade down through the view model into the view.

Cons

Using MVVM on its own, it simply moves the massiveness from the view controllers to the view models. Instead of massive view controllers, you have massive view models.

results matching ""

    No results matching ""