Swiftui learning-1 understanding of MVVM

Swiftui learning-1 understanding of MVVM


Originally, we have been developing with Objective-C, and the application itself is based on MVC to build the code structure. Recently, we learned that swiftui must comply with MVVM to work, so we began to understand this concept deeply. Mainly through the open class of Stanford University, summarize the important knowledge points. Link below:


The main differences between MVC and MVVM developed by IOS

MVC is a model view controller structure, which is used in the traditional UIKit framework.It doesn’t mean that UIKit must use MVC, but it has a high degree of matching with MVC.

  • MVC’s model is generally relatively simple, just creating a data object format. The encapsulation, operation and management of data are generally executed in the controller.
  • MVC’s view performs view construction, rendering and event response processing, and provides some refresh methods for the controller to use.
  • The controller is responsible for updating data, logic and view. The relationship with view and model is one-way communication, and the controller performs command to view(imperative)Command operation.

give an example:
The emperor in the Empire issued various orders to direct others to do things. That’s how a country works. But the disadvantages are obvious. Because the orders are issued one by one, one of the most unfavorable reasons is time.
In UI development, this kind of command execution, the function is called over time: the button is put here, the label is put there, this will be done later, and then that will be executed. Maybe others can call the function to change the UI at any time, so we should always be vigilant.
At this time, we need a timeline to understand the order of calling functions, and we can’t prove that the interface can work truly and effectively when calling any function in any order. Because under the command operation, all instructions cannot be executed at the same time.

MVVM is a model view ViewModel structure, which is used in swiftui framework.Swiftui is responsive programming(reactive programming)。

  • The model of MVVM is completely independent of the UI and handles data and logical operations. Data flow(data flows)Is read-only during mapping to a view.
  • MVVM’s view is declarative(declarative)View, the methods we declare for the UI, do what they should do at any time. You don’t need to care about any state changes(state changes)The changes in the model view are only the changes in the data recorded in the model view.

The code in stuct structure is actually read-only. When calling, it is impossible for others to change the code in the function. What they see is declared.

  • ViewModel execution interpretation(interpreter)Work, bind the view to the model. ViewModel focuses on changes in the model(notices changes)And then publish these changes(publishes changed), subscription(subscribes)A release was(publication)Your view will change.

It is important to understand that the ViewModel does not directly point to the view pointer and does not directly talk to the view. If the view subscribes to a publication, it will ask how the ViewModel adapts to the current changes. This process will not return to the model, because the role of ViewModel is to explain the changes of the model.

MVVM processes intent

MVVM has a corresponding associated architecture, which is model view intent. If the user intends(intent)Do some operations, then these intents need to carry out the reverse transfer process from view to model. Swiftui has not yet been designed, so we use the following series of operations to deal with intent:

  • View Calls Intent functionView call method
  • ViewModel modifies the ModelView model modify model
  • Model changesModel changes
  • ViewModel notices changes and publishesThe model focuses on changes and releases them
  • View whitch subscribes Reflect the ModelSubscribe to changing views for model mapping

Compared with the mapping process of MVVM, the ViewModel handles the view operation and modifies the model.


OC, as the first IOS development language used for decades, is indeed cumbersome among various emerging front-end languages. Swift has gradually iterated over several years, and replacing OC has become an irresistible historical wave.
Even if OC is such a powerful language, it will soon be eliminated without progress. Besides, we are just brick movers of the times. We must constantly learn new content.