Item Development, often encountered an interface is very complex, various control various View, with each other complex interactive logic, the data of each component sometimes need specialized server interface support .
The MVC pattern puts all this logic into C , making a viewcontroller very large and complex. Whether it's a change or debug, it's time consuming.
This can be improved by introducing the M-V-VM pattern into MVC . The structure is as follows:
explain :
viewmodel< Span style= "font-family: Song body, SimSun; font-size:14px; " > viewcontroller Create and manage these viewmodel
ViewModel manages refresh and user action events for View. If the user's response requires other components to interact, pass the delegate up to Viewcontroller. Communication routing between ViewModel is performed by Viewcontroller .
View provides an API for users to refresh the display, and the concrete refresh of inner elements is not visible externally.
In addition to refreshing the display, View also provides a response to user actions, which is handled by delegate notification ViewModel .
Datamodel is managed by ViewModel . It has the ability to get the data of the View, to save the data to a structure. A common property is typically created to hold the new data obtained. ViewModel establishes KVO monitoring for this attribute .
We typically use asynchronous methods to download new data from the network, and when the new data is downloaded successfully, it is parsed and stored in public properties. At this point,the KVO method is activated, and the value of the ViewModel read property is passed to the View, which refreshes the display .
Datamodel provides an update data method similar to Reloaddata, called by ViewModel . ViewModel As long as this method is called, everything starts to run automatically .
Datamodel is only visible to ViewModel . In this way, the three have formed a relatively independent ecological circle, the outside can only see ViewModel. ViewModel provides external APIssuch as refreshing data, submitting cross- ViewModel communication requests, and more .
Demo Project is here
The application of M-V-VM in the development of iOS