iOS framework MVC+MVVM combined in combat

Source: Internet
Author: User

The role of the framework for the entire application is very important, remember a friend said: with what frame ah, a good package is not OK? But my understanding is that the good package can definitely do more with less, but if you do not follow a certain rules of encapsulation will be a bit difficult to understand, the maintenance of the code is crazy, I think the architecture is to specify how to encapsulate.


After reading the idea of the Great God's framework, I decided to practice it in our project. Just arrived at a new company, the company's code is very rotten, no design ideas, eventually led to the controller class code to reach 2000 lines, up to 3,000 lines, very bad for the code reuse, originally very similar interface, inherit something that can be done, unexpectedly achieved two times, There are more things that can be used in public, so my idea of optimizing the code is aroused.


Our program runs on the ipad, so there are more controls and events per Viewcontroller, and the resulting code is focused on the VC, and after reading the misunderstood MVC and deified MVVM, we decide to use the optimized MVC.


View, like the analysis view of the great God is a layer, there may be more than one class, our interface was written by storyboard, I am not going to use pure code to implement it again, so add a Viewutil class, Let all the attributes on the view also act as Viewutil properties, which I am going to use to listen to the properties of ViewModel (for reference to MVVM), and once the properties of the ViewModel are changed, the view is changed. The change of view is implemented in the Viewutil class. This way the view layer (Initialize and change state) has a place to deal with.


ViewModel, using MVVM can make the architecture more clear-minded. The model we normally use for data transmission is used to store the data requested on the network, not necessarily corresponding to the element one by one on the page. For example, there is a switch on the page, to control the display of something and hidden, the model will not be stored, this time we create a bool property in ViewModel Showsth, that is, a page of all the variable elements and ViewModel attribute one by one corresponding, In this way we modify the Showsth=yes in the Viewcontroller, because the view listens to this property of ViewModel, let something show. The logic becomes clear and the interface is transformed into specific properties to manipulate.

ViewModel is also used to store data (for example, an array of results from a network request) and to process logic (ordering something), but does not include network requests, because the network request is also put in ViewModel, ViewModel will become the second Viewcontroller, is not conducive to the control of the process, there is a large number of code transferred from Viewcontroller to ViewModel.


Viewcontroller, Viewcontroller or used to receive all the interaction, but did a relay role (as to how to do the relay, the following will be introduced), to other classes to deal with, This can reduce the code in Viewcontroller, but also can put all the interaction processing in one place, you can easily debug quickly find the problem.


Service, This class handles network requests, makes decisions about success and failure, and can process data into types required by the ViewModel class, such as arrays containing the corresponding types of model.


model, the class of data storage, is the model we usually use, you can do data persistence processing.


So a complete interactive process is probably like this, for example, the view received a click on a button event, view with a proxy callback to Viewcontroller processing, such as to request the data to display, call the service class to make a network request, Return the results and then return to the Viewcontroller, and then viewcontroller the data assigned to ViewModel properties Datasourcearray inside, this time because the view monitoring Datasourcearray this property, View will call TableView directly to refresh. This is the interactive flow of the architecture.


The refresh of view and view is placed on the view layer, the network request is independent, the viewcontroller handles all things, the ViewModel handles the arithmetic logic, and the element of the page is represented and controlled, which is the basic class responsibility of the whole architecture.


Why not let Viewcontroller directly call the view method to update after the request succeeds, you can also do so, because I understand the framework is not deep enough, so I can only answer. 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0077.gif "alt=" J_0077.gif "/>


If idle TableView is in the way, you can deal with it:

    • Separate the UITableView Data Source into a different class.

Others summed up the method, has not been practiced, just feel so divided a bit more chaotic. And so there is time to practice.


One of the Viewcontroller in our project was reduced from 1500 lines to 800 lines, although less is not too much, but the sense of logic is clearer, the new things will have a clear place to deal with, feel more normative.


Finally feel the use of architecture or sub-projects and sub-pages, a relatively simple interface without what architecture, complex can try to divide. The understanding of the framework is relatively superficial, there may be a lot of understanding of the wrong place, will be corrected later.


Resources:

Misunderstood MVC and the deified MVVM


Ape Question Bank IOS client Architecture Design


On the architecture design and team collaboration of MVVM in iOS

iOS framework MVC+MVVM combined in combat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.