Preface I have been studying the architecture of iOS for some time. Why? The company's architecture has always been MVC. When we officially launched, the project already had over 100,000 lines of code. The main VCs generally have more than 2,000 lines of code. The key is that we have only done one-third of the business logic in the current version... So, restructure it. text MVVM MVVM: Model-View-ViewModel MVVM is actually an evolved version of MVC. It decouples business logic from VC to ViewModel to achieve a major "slimming" of VC. Explain with code! Make a simple login judgment: Create LoginViewModel (logic processing), LoginModel (only data), and LoginViewController. LoginView is not used here to allow beginners to better focus on decoupling with ViewModel. Of course, if you understand all of this, you can directly look at Wzxhaha/RandomerFramework, which is the basic architecture (SubClasses+Protocol+MVVM+RAC) of the independent project Randomer that I am working on, as well as its login and registration module. In addition, thanks to Wang Longshuai for this article, which opened the door to a new world for me. Add method to LoginModel
There is nothing much to say about this, just add an initialization method to the Model. Add method to LoginViewModel
Add a binding model initialization method to ViewModel, as well as a method to determine whether the account password is valid. Then VC (or View) can directly obtain the result of the judgment
It doesn't matter when it comes to simple functions, but when you are dealing with complex logical judgments, MVVM will have huge advantages. By the way, let me talk about ReactiveCocoa. The reason why I advocate MVVM so much is mainly because RAC and MVVM are a perfect match! ReactiveCocoa RAC has the characteristics of functional programming and responsive programming. If you are not familiar with programming ideas, you can read my WZXProgrammingIdeas The main purpose of RAC is to monitor various events. RAC calls this a signal flow, and then receives the signal through block callback. A large number of blocks are used in it, so be sure to use @weakify(self) and @strongify(self) well. Why do we say that RAC and MVVM are a perfect match? MVVM decouples methods to ViewModel, but they still need to be called by VC (V), so the logic for determining when to call will still be complicated. RAC solves this problem. It is responsible for monitoring events and then calling ViewModel to perform logical judgment. For example:
RAC monitors the login and registration buttons, making the code concise and the structure very compact. For a demo, check out this one: Wzxhaha/RandomerFramework https://github.com/Wzxhaha/RandomerFramework Or the simple version of WZXRACDemo https://github.com/Wzxhaha/WZXRACDemo Chained network request framework Why encapsulate WZXNetworking This is a framework with very scary fault tolerance.
Except for .setRequest(url) and startRequestWithSuccess failure methods, the others are not necessary. You can do this:
Chaining shows amazing advantages when there are many parameters and parameter choices or when there is a high probability of modification. Because it is very convenient to modify, just add or modify a method. For example: The centralized API package should look like this:
When you want to add a Version attribute to determine the API version, what can you do? You can only rewrite the method, add a Version parameter to the method, and then change the method for all network requests used. We will not consider the comparison if we switch to distributed API encapsulation.
Is this structure too loose? Then change to WZXNetworking All we need to do is add another method and a member variable, and then add .method() after the original method.
The code is here: WZXNetworking https://github.com/Wzxhaha/WZXNetworking As for how the chain is implemented, see WZXProgrammingIdeas https://github.com/Wzxhaha/WZXProgrammingIdeas |
<<: Learn common Mac commands to help iOS development
>>: Some methods to detect iOS APP performance
At the age of 18, he worked as an assistant Fight...
On July 30, the Dalian Municipal Government Infor...
What is the largest hydroelectric project in the ...
In addition to delivering a good product, how can...
Chapter 0: Introduction to New Media Operation To...
Editor's Note: Since its listing , Bilibili (...
According to the mobile advertising intelligence ...
Talking about cervical cancer Is HPV the first th...
September 10, 2024 is the 22nd World Suicide Prev...
July 10 to 16 this year is the 33rd National Ener...
From November 4 to 8, my country just experienced...
Course Outline 00 The five most common mistakes C...
Tesla's autonomous driving has always attract...
[[125173]] Today, the vulnerability reporting pla...
Google has made a difficult decision: to deprecat...