Some thoughts on MVC and MVVM in iOS

Source: Internet
Author: User


Things from the general development of a massive Viewcontroller said, a huge VC generally less than thousands of lines of code, more than tens of thousands of lines.
In this case, the maintenance of the code is a deadly barrier to personal experience.
When you try to find a Web request from 6000 lines of code and find the relevant implementation logic, it can be confusing.
Further, if you're going to test a certain logic, a scenario, it's a very difficult thing to do.
Furthermore, if you want to reuse a part of the scene logic, it is almost impossible, because all the code is coupled in a VC.


Why do you create a VC code so much, so complicated?
Generally there are the following reasons:
1.view construction: Whether using IB or handwriting interface, the beginner is generally to put the interface building code in the VC, even in the VC life cycle function, this is stepping out massive viewcontroller first step.
2. Network data request and subsequent processing: Network request is a more critical place, involving the processing of data. We do a discussion in the MVC mode, first of all, view,view as an interface implementation object, generally can use MVC people will not put the network request inside, this skip. Next is the model, in general MVC models as an entity class, for storing entity information, performing certain behavior of the entity, the operation of the local database, if the network request is put into the model execution, the network data needs to be executed asynchronously, if the model life time than the network data request for a long time, That's fine, but the general server returns the JSON data contained in the entity information very much even some irrelevant model of the request, this part is a problem, so the model do network seems out of tune, finally can only use VC, this is massive viewcontroller second step.
3. Response logic: In the VC often there will be a user event to make the corresponding code and delegate method, this part of the code often contains complex judgment logic and data processing code, this is the third step of massive viewcontroller.
4. Data source method: Typical TableView will have DataSource method, the number Cellforrowatindexpath most typical, in which the code before the data presentation is also very much, massive viewcontroller fourth step.
5. Local database operation: Not much explanation, massive viewcontroller fifth step.
6. Other non-VC code: Other, such as a bit of tool nature and other code. So 66, a massive viewcontroller formed.


There are so many reasons, just to explain the origin of a massive viewcontroller, like an analysis of a person's success story.
So the next point is how to solve this annoying massive viewcontroller, in order to achieve a lightweight viewcontroller.


Since there is a reason, then the solution from the cause of it, the sequence is uncertain, combined with the MVC model (Model+view+controller), do not understand the Google itself.
1.view Processing: Personal humble opinion, the general practice is to separate a major view class from the VC, if there are other complex view in the view class, then continue to detach. We can simulate the practice of uikit, a principle is the large view structure imaging tableview and other views, such as a small look constructed as a control such as Tabbar, and then the related views may make the click Effect and implementation logic encapsulated in the class, set aside the interface, custom some protocol, Execute Proxy method Data source method, so that not only reduce the burden of viewcontroller, but also greatly improve the package and reusability of the view object, such as you want to use the same view on different VCs, then you do not have to rewrite, just according to the existing interface and agents, Data source method to implement the relevant logic.


2. Processing of the network data, this is put back in the MVVM discussion.
4. Data source method: Continue to separate the responsibilities of the VC, separate the function of a single datasource, and then transfer the relevant data in the past, the relevant processing logic block passed the past. The rest of the data Processing section still leaves the surface mvvm introduction.


5. Local database operation: In the MVC mode, this part of the code should be encapsulated in the model, in the VC call the relevant interface.


3. Response logic: Through the encapsulation of the view, the relevant response method has been relatively clear, but the response logic is not processed, in fact, it is only the data processing, because the view display effect has been encapsulated in the view class. Operations on local data can be performed by invoking the model's method.


6. For some unrelated VC code, if can be reused, then should be added in the project in the Util directory, in order to do the tool class use.


The following describes the MVVM pattern, MVVM = model + View + ViewModel, and of course there is a viewcontroller in iOS.
Compared to MVC,MVVM more than a viewmodel, and the focus is viewmodel.
As the name implies, ViewModel is the view model, to display the data on the image processing, the general process is to accept the VC event command request and processing related data, after the end of the standard display data processing good to the VC display to the view, this predicate view model. Separation of the view model, similar to the view class approach, set aside the operation interface, protocol and proxy, so that the data layer can be reused, as long as the VC conforms to the relevant protocol, then in different VC can use the same viewmodel. Encapsulation and reusability are reflected and easy to test.


The answer to the person who borrowed the crooked fruit is:
Views display a certain shape of data. They has no idea where the data comes from.
ViewModels hold a certain shape of data and commands, they does not know where the data, or code, comes from or what it is di Splayed.
Models hold the actual data (various context, store or other methods)
Controllers listen for, and publish, events. Controllers provide the logic that controls what data is seen and where. Controllers provide the command code to the ViewModel so, the ViewModel is actually reusable.


In this way, we can put the validation of data validation, view of the display logic, network data requests and processing, and other data processing logic set into ViewModel.
The issues above are resolved:
2. Network data processing: In the ViewModel to build the relevant network request logic and the loaded data processing and local preservation, using proxy method to notify the VC execution data display.
4. Data source method: In ViewModel we can construct some interfaces to encapsulate the presentation logic in the Heightforrowatindexpath,numberofrowinsection method, and then encapsulate the code of validation data.
5. Local database operations: As the intermediate hub connecting model and VC, the operation of the local database should be encapsulated into the data processing logic in ViewModel.


The introduction of MVVM and the solution to the massive viewcontroller problem is probably so much. Attach some introductory links:
objc.io,http://www.teehanlax.com/blog/model-view-viewmodel-for-ios/


Say so much, say something summarizing.
The differences and similarities between MVC and MVVM.
Difference:
Pros: MVVM is a view model ViewModel based on MVC, used for validation of data validation, view presentation logic, network data request and processing, other data processing logic sets, and setting related interfaces and protocols. Compared with the MVC,MVVM of VC in the responsibility and complexity of smaller, more convenient testing of data processing logic, the cause of the bug to troubleshoot more convenient, code readability, reusability and maintainability is higher. MVVM is less coupled. MVVM's responsibilities at different levels are clearer and more conducive to code authoring and team collaboration.
Disadvantage: MVVM has an increased amount of MVC code compared to. MVVM requires a clearer pattern of ideas than MVC before code is written.




Common:
Pros: There are model,view,viewcontrolelr three basic levels, which are good for view encapsulation. Have a certain degree of readability. are low-coupling. is helpful for code maintenance. The responsibilities of each part are clear, easy to test, the big solution is small, facilitate the team to work together.
Cons: So-called MVC and MVVM are not clearly defined, especially on different platforms, and 1000 people have 1000 MVC and MVVM understandings, which makes understanding difficult. The code will increase by a certain amount compared to the previous one.


This article summarizes:
Create a lightweight viewcontroller from the massive VIEWCONTROLELR problem description, analyze the reasons, and then put forward to the VC separation of duties, such as the separation of model,datasource,util tools, etc. Then go to MVC to discuss the solution to the problem, and then to the MVC upgrade version of MVVM problem solving ideas. In general, a lightweight viewcontroller constructs the idea that there is a reason why MVC and MVVM have pros and cons, but the drawbacks are trivial in the face of the benefits they bring. Their low coupling, encapsulation, testability, maintainability, and multi-person collaboration facilitate a significant increase in open efficiency.
In a nutshell, a lightweight viewcontroller is built on the separation of code responsibilities based on the MVC and MVVM patterns.


Time is limited, the code is not posted.
Related references and links are attached
1.objc.io
2.http://www.teehanlax.com/blog/model-view-viewmodel-for-ios/
3.stackoverflow:different between MVC MVVM, advantage of Mvc,advantage of MVVM
Open source project in 4.GITHUB:C-41,MVVM mode

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Some thoughts on MVC and MVVM in iOS

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.