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
I have read an article published by Diao Ye befor...
What is the user operation system like? I believe...
Kuaishou is a platform with down-to-earth short v...
【51CTO translation】The past few years have witnes...
Every time this season comes, many people develop...
Take a few minutes to look at the following three...
According to industry insiders, mini programs wil...
: : : : : : : : : : : : : : : : : : : : : : : : : ...
From the lens to the living room, as the only ful...
The concept of Wancibaping promotion: (WeChat: 18...
This article introduces a simpler, easier-to-use,...
[[131068]] On April 1, data from the Ministry of ...
After the first generation of Tmall Magic Box and...
The ancient ship "Yangtze River Estuary No. ...
On January 13, Tencent announced that the "M...