loading

Model View View-Model

Model View View-Model

The MVC design pattern, which is easier to use and comprehend, was covered in the preceding section. Nevertheless, there are certain drawbacks to the MVC, which prompted the development of the Model View View-Model design pattern. We will go into great detail about each of the MVVM components in this section of the lesson. We will also examine the Model View Controller design pattern’s drawbacks.

What is MVVM?

The iOS architectural design pattern known as MVVM (Model View View-Model) divides items into three primary components.

  • Model: The application data is contained in it. Data persistence and parsing fall under its purview.
  • View: It is in charge of projecting the model data, together with the controls and visual features, onto the screen. It is an extremely reusable UIView subclass.
  • ViewModel: This contains the business logic that feeds the model data to the view once it has been transformed into values. All business logic, including data manipulation, occurs at ViewModel in MVVM.
Model View View-Model -
  • The MVC design pattern gains a new component called ViewModel from the MVVM, which allows business logic to be handled at ViewModel and reduces the need for the view controller.

Problems with MVC

Data formatting is the MVC’s most frequent issue. The data for the application is contained in the model in MVC. Formatting data before displaying it to the user is one of the criteria for designing iOS applications.

Let’s say that an iOS application has to show the user the date that was returned by the server. On the other hand, how the user would comprehend the date format if the server sends a date in a lengthy millisecond format and we show the user the precise date that the server returns? As a result, date formatter must be used to format this date into the string.

Where the formatting happens in MVC is the issue. It is not necessary to inform the model about the data that is being shown to the user. Since the controller is already heavy due to having so much business logic, including the data source and delegate function, it is the only object that can be used to store the formatting code since the model does not need to understand the data displayed to the user. Because the Controller handles all data manipulation activities, the Controller becomes quite heavy in MVC.

Where to use MVVM

The issue of a hefty controller in MVC is resolved by the MVVM. It gives the scheme a fourth element, which is ViewModel. By sending the model’s data to the view through a view controller, the ViewModel not only maintains the model but also oversees the data manipulation responsibilities. When we need to change models into a different representation for a view, we can apply this pattern. The view model can be used, for instance, to turn a decimal value into a currency string, or to convert a date object into a formatted string. This design works well with MVC. By including an additional component, the role of a view controller is intended to be minimized. The view controller still handles a lot of the heavy lifting, though, such as creating outlets and inserting formatted data into them.

The MVVM Project Architecture

The architecture of an XCode project that uses the MVVM design pattern is shown in the following image.

Model View View-Model -
Share this Doc

Model View View-Model

Or copy link

Explore Topic