Differences between MVVM and MVC

1. MVC

The disadvantages of the MVC

  • Heavy View Controller
  • M: The object of the model is usually very simple. According to Apple’s documentation, the Model should include the data and the business logic that manipulates the data. In practice, the Model layer tends to be very thin, and the business logic of the Model layer should not be dragged into the Controller anyway.
  • A view is usually a collection of UIKit controls (Component) or encoded UIKit controls. There is no need to let Controller know how the View is constructed (PS: IB or handwriting interface), and the View should not refer directly to the Model (PS: real life, you know!). And reference the Controller only through the IBAction event. Business logic is clearly not subsumed in a View; the view itself has no business.
  • C: controller Controller. The Controller is the app’s “glue code” : coordinating all interactions between the model and view. Controllers manage view hierarchies of the views they own and respond to loading, chatting, baying, etc. They are often filled with model logic of models we don’t want to expose and business logic we don’t want to expose to views. Requests for and subsequent processing of network data, local database operations, and some instrumental assistance methods all augment Massive View Controller generation.
  • Missing network logic The MVC definition used by Apple says that all objects can be classified as a Model, a View, or a Controller.

You might try to put it in a Model object, but that can be tricky because network calls should be asynchronous, so if a network request lasts longer than the Model that holds it, things get complicated. Obviously, it’s even more out of place to do network requests in a View, so you’re left with Controller. If so, this exacerbates the Massive View Controller’s problems. Otherwise, where is the home of network logic?

  • Poor testability

Because the View Controller is a mix of View processing logic and business logic, unit testing to separate these components is a daunting task.

2. MVVM

A good way to solve the Massive View Controller problem is to extract the presentation logic from the Controller and place it in a special place, the viewModel. Derived from MVC, MVVM is an evolution of MVC that facilitates the separation of UI code from business logic. It formalizes the tightly coupled nature of views and controllers and introduces new components. The structural relationship between them is as follows:

2.1 Basic Concepts of MVVM
  • inMVVM,viewview controllerFormally linked together, we treat them as one component
  • viewview controllerNeither can be quoted directlymodel, instead referring to the viewmodel (viewModel)
  • viewModelIs a place to put user input validation logic, view display logic, initiate network requests, and other code
  • useMVVMThis slightly increases the amount of code, but overall reduces the complexity of the code
2.2 Precautions for MVVM
  • viewreferenceviewModel, but not the other way aroundviewModelThe introduction of#import UIKit.hAny reference to the view itself should not be placedviewModel(PS:Basic requirements must be met)
  • viewModelreferencemodel, but not the other way around * MVVM usage recommendations
  • MVVMIt’s compatible with what you’re currently usingMVCArchitecture.
  • MVVMIncrease the testability of your application.
  • MVVMIt works best with a binding mechanism (PS:ReactiveCocoaYou deserve it.
  • viewControllerTry not to involve business logic, letviewModelDo these things.
  • viewControllerJust a middleman, receivingviewEvent, callviewModelThe method and response ofviewModelThe change.
  • viewModelYou must not include viewsThe view (UIKit. H)“Or withviewThis creates coupling, which is not easy to reuse and test.
  • viewModelThere can be dependencies.
  • viewModelAvoid being too bloated or repeat yourselfControllerIs difficult to maintain.
2.3 Advantages of MVVM
  • Low coupling:ViewCan be independent ofModelChanges and modifications, oneviewModelCan be bound to differentView
  • Reusability: You can put some view logic into oneviewModelInside, let a lotviewReuse this piece of view logic
  • Independent development: Developers can focus on business logic and data developmentviewModelDesigners can focus on page design
  • Testable: Interfaces are often difficult to test, whileMVVMPatterns can be targetedviewModelTo test it out
2.4 Disadvantages of MVVM
  • Data binding enablesBugIt’s hard to debug. You saw something wrong with the interface. It could be youViewThe code isBugOr it could beModelThere is a problem with the code. Data binding makes a locationBugBeing quickly transferred to other locations, it becomes less easy to locate the original problem.
  • For large projects, data binding and data transformation require more memory (cost). The main costs are:
  • The cost of converting the contents of an array is high: each item in the array needs to be converted toItemObject, if you have an Item object that looks like an array, it’s a headache.
  • In most cases, the transformed data cannot be displayed directly. In order to be displayed, a second transformation is required.
  • Only when the data returned by the API is highly standardized can these object prototypes (Item) is highly reusable, otherwise prone to type explosion, increase maintenance costs.
  • Viewing the data content through object prototypes is not as good as viewing it directly during debuggingNSDictionary/NSArrayIntuitive.
  • When data from the same API is presented by different views, it is difficult to control the code that converts the data, and it can be scattered wherever it is needed.
3. Summary
  • MVCDesign mode is not a terminally ill, incurable architecture, at least at present MVC design mode is still the mainstream framework of iOS development, its existence is reasonable. There are still many possible ways to avoid and solve the drawbacks described in this article, and build a lightweightViewController.
  • MVVMisMVCWhile MVVM promotes the separation of UI code from business logic, it is somewhat mitigatedViewControllerIs bloated, butViewandViewModelThe data binding between the two makes MVVM complicated and difficult to use. If we can’t better manage the data binding between the two, it will also cause the problem that the Controller code is too complex and the code logic is difficult to maintain.
  • A lightweightViewControllerIs based onMVCandMVVMPatterns are built for the separation of code responsibilities. MVC and MVVM have both advantages and disadvantages, but the disadvantages are insignificant when compared to the benefits they bring. Their low coupling, encapsulation, testability, maintainability and multi-person collaboration greatly improve the efficiency of the method.
  • At the same time, we need to maintain a heart to embrace change, and rational analysis of the attitude. In the face of new technology, we should not blindly follow or be old-fashioned. All decisions should be based on careful analysis so that we can cope with technological changes.

More: a collection of iOS Interview questions