Skip to main content

MVVM and MVC in swift

MVC: 

  • View is supposed to be inactive and only displays prepared data on demand.
  • Model: It represents simple data.
  • Controller should work on the Model data to prepare it for the Views, which then display that data.
  • View is also responsible for notifying the Controller about any actions, such as user touches.
  • UIViewController is supposed to be a Controller in the MVC pattern, 


MVVM: 

The Model View ViewModel (MVVM) is an architectural pattern used in software engineering.

  • Models hold application data / Content. They’re usually structs or simple classes. 
  • Views display visual elements and controls on the screen. They’re typically UIView and UIViewController. xib and storyboards
  • View models transform model information into values that can be displayed on a view. They’re usually classes, so they can be passed around as references.
    • ViewModel hides all asynchronous networking code, data preparation code for visual presentation, and code listening for Model changes. All of these are hidden behind a well-defined API modeled to fit this particular View.
    • Test covering in  ViewModel is rather easy as it does not contain any reference to a view. Rather it only prepares data for the view only. It is the view that pulls the data from the view model.  Also, the view delegates the action to the View model.Since ViewModel is pure NSObject (or struct for example), and it’s not coupled with the UIKit code, you can test it more easily in your unit tests without it affecting the UI code.
    • Application is easer to maintain and extend.
    • Application is easier to unit test.


the View (UIViewController/UIView) has become much simpler while ViewModel acts as the glue between the Model and View.


Comments

Popular Posts

escaping closure and closure

Certificate pinning and Alamofire