Introduction to MVVM Architecture Design and team collaboration in iOS development

Source: Internet
Author: User

Introduction to MVVM Architecture Design and team collaboration in iOS development
I want to write this blog today to serve as a reference. I want to share my thoughts with you and learn from each other. I hope you can criticize and correct the shortcomings in this blog. The content of this blog follows the style of previous blogs. It is also dominated by dry goods, and occasionally abusses the salted eggs (haha ~ It's not good. I started to post a blog again ~). My project experience is limited, and my understanding of architecture design is limited. My personal understanding of MVVM is mainly based on the previously used MVC Web framework ~ I used ThinkPHP framework in school and SSH framework. They are both MVC Architecture models. Today, MVVM is very similar to traditional MVC, and can be said to be sibling, that is, the family. Speaking of Architecture Design and team collaboration, this is important for App development. Even as a professional bricklayer player, the premise is where are you finished? Not only does Code have a framework, but other things have a framework, such as bridges and so on ~ I won't talk about it here. A good engineering framework can improve the efficiency of team collaboration and reduce code redundancy and coupling. reasonable division of labor and system architecture design are indispensable. As for team collaboration, it is not just about using version control tools such as SVN or Git. As for how to use SVN in iOS development, refer to the previous blog (Version Control for iOS development (SVN )). A team can work efficiently. I think communication is the most important thing. Everyone in the team is very friendly, in addition, there are no barriers to communication (but some teams always have a few people that cannot be combined). Communication is the most important thing in the team. As for how to use SVN, that's not the case! Let's talk about architecture design and team collaboration in iOS development with a Demo I wrote today. Let's get started with today's topic. I spent some time on a project to write my blog today. The interface at the project background was tested using the Sina Weibo API, this article will be followed by the GitHub sharing link. OK ~ In terms of height, the benevolent wise sees wisdom, exchanges ideas, and learns together. I. I would like to have a look at MVVM. I will not go into details about MVC here, but also about MVVM again. I will give a brief introduction here when I click on Baidu to catch the ghost. The following Demo uses the MVVM architecture mode. The Model layer is indispensable. We need something to act as a DTO (data transmission object). Of course, it is also possible to use a dictionary. Should we be flexible in programming. The Model layer is a relatively thin layer. If you have learned Java, you should be familiar with JavaBean. The ViewModel layer is the binder of the View and Model layers. It is an excellent place for placing user input verification logic, View display logic, initiating network requests and various other code. To put it bluntly, the business logic and page logic of the original ViewController layer are stripped out and placed on the ViewModel layer. The View layer is the ViewController layer. Its task is to obtain data from the ViewModel layer and then display it. In the face of MVVM, we should first make a simple introduction. If you have a good understanding and application, you have to practice it. 2. The discussion about whether or not StoryBoard is used in the project is often seen on the Internet that StoryBoard or Xib is not recommended, and it is recommended to write it in pure code. I personally think this idea is contrary to the original intention of Apple to design StoryBoard. In my previous project, I mainly used StoryBoard, supplemented by xib, and then integrated each StoryBoard with code. let's take an example of the advantages of using Storyboard. It's okay to add constraints to the control. If Storyboard is used to complete the tasks in minutes and seconds, then how disgusting it is to use the code to add constraints, if you write code only, it will spend a lot of time writing the UI, and the technical content is relatively low. This individual thinks it is unnecessary. No one in the team is responsible for UI development. Developers only need to be responsible for one Storyboard. When submitting the code using SVN, remove the following check box (for example, in this way, Storyboard is no problem. Then you can use the code for integration. If you add a new resource file to your Project, use the SVN file that comes with XCode to submit the file together with the Project Setting file. Iii. MVVM (the Group created with Xcode is a virtual folder. To facilitate maintenance, we recommend that you create a physical folder and then introduce it manually) 1. the following is an example of the MVVM architecture model. The following is the project's level-1 directory. The interaction between each layer is described in the project directory in the form of blocks: 1. request: the class that stores network requests in the folder. The specific implementation 2 is given below. config: The project configuration file 3. resource: it is the Resource file of the project. There are image resources and Storyboard file resources below. 4. tools is a tool File class that stores tool classes, such as regular data matching. 5. vender: stores third-party class libraries. 6. model: This is not much to say. 7. viewController: stores ViewController class resource files, that is, View layer 8. viewModel: stores various business logic and network requests. detailed Request: the Request is responsible for network requests. The details are as follows: NetRequestClass is used to store the network Request Code, because this project is only a Demo, therefore, only the methods for monitoring the network status, GET requests, and POST requests can be encapsulated as needed. NetRequestClass. the code in h is as follows: 1 // 2 // NetRequestClass. h 3 // MVVMTest 4 // 5 // Created by Li zeru on 15/1/6. 6 // Copyright (c) 2015 Li zeru. all rights reserved. 7 // 8 9 # import <Foundation/Foundation. h> 10 11 @ interface NetRequestClass: NSObject12 13 # connectivity of The pragma monitoring network 14 + (BOOL) netWorkReachabilityWithURLString :( NSString *) strUrl; 15 16 # pragma POST request 17 + (void) parameter: (NSString *) parameter WithParameter: (NSDictionary *) parameter19 parameter: (ReturnValueBlock) block20 WithErrorCodeBlock: (ErrorCodeBlock) errorBlock21 parameter: (FailureBlock) failureBlock; 22 23 # pragma GET request 24 + (void) parameters: (NSString *) parameters WithParameter: (NSDictionary *) parameter26 parameters: (ReturnValueBlock) block27 WithErrorCodeBlock: (ErrorCodeBlock) errorBlock28 WithFailureBlock: (FailureBlock) failureBlock; 29 30 @ end NetRequestClass. the code in m is as follows: 1 // 2 // NetRequestClass. m 3 // MVVMTest 4 // 5 // Created by Li zeru on 15/1/6. 6 // Copyright (c) 2015 Li zeru. all rights reserved. 7 // 8 9 # import "NetRequestClass. h "10 11 @ interface NetRequestClass () 12 13 @ end 14 15 16 @ implementation NetRequestClass 17 # pragma monitoring network connection 18 + (BOOL) netWorkReachabilityWithURLString :( NSString *) strUrl 19 {20 _ block BOOL netState = NO; 21 22 NSURL * baseURL = [NSURL URLWithString: strUrl]; 23 24 AFHTTPRequestOperationManager * manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL: baseURL]; 25 26 NSOperationQueue * operationQueue = manager. operationQueue; 27 28 [manager. reachabilityManager setReachabilityStatusChangeBlock: ^ (AFNetworkReachabilityStatus status) {29 switch (status) {30 case failed: 31 case failed: 32 [operationQueue setsuincluded: NO]; 33 netState = YES; 34 break; 35 case AFNetworkReachabilityStatusNotReachable: 36 netState = NO; 37 default: 38 [operationQueue setSuspended: YES]; 39 break; 40} 41}]; 42 43 [manager. reachabilityManager startMonitoring]; 44 45 return netState; 46} 47 48 49 /********************************** * ***** 50 make a judgment here. If there is an errorCode 51 in dic, call errorBlock (dic) 52. If there is no errorCode, call the block (dic 53 ******************************/ 54 55 # pragma -- mark GET Request Method 56 + (void) parameters: (NSString *) requestURLString 57 WithParameter: (NSDictionary *) parameter 58 parameters: (ReturnValueBlock) block 59 WithErrorCodeBlock: (ErrorCodeBlock) errorBlock 60 WithFailureBlock: (FailureBlock) failureBlock 61 {62 minutes * manager = [[AFHTTPRequestOperationManager alloc] init]; 63 64 AFHTTPRequestOperation * op = [manager GET: requestURLString parameters: parameter success: ^ (AFHTTPRequestOperation * operation, id responseObject) {65 NSDictionary * dic = [NSJSONSerialization JSONObjectWithData: responseObject options: NSJSONReadingAllowFragments error: nil]; 66 DDLog (@ "% @", dic ); 67 68 block (dic); 69 70} failure: ^ (AFHTTPRequestOperation * operation, NSError * error) {71 failureBlock (); 72}]; 73 74 op. responseSerializer = [AFHTTPResponseSerializer serializer]; 75 76 [op start]; 77 78} 79 80 # pragma -- mark POST Request Method 81 82 + (void) NetRequestPOSTWithRequestURL: (NSString *) requestURLString 83 WithParameter: (NSDictionary *) parameter 84 WithReturnValeuBlock: (ReturnValueBlock) block 85 WithErrorCodeBlock: (ErrorCodeBlock) errorBlock 86 WithFailureBlock) failureBlock 87 {88 minutes * manager = [[AFHTTPRequestOperationManager alloc] init]; 89 90 AFHTTPRequestOperation * op = [manager POST: requestURLString parameters: parameter success: ^ (AFHTTPRequestOperation * operation, id responseObject) {91 NSDictionary * dic = [NSJSONSerialization JSONObjectWithData: responseObject options: NSJSONReadingAllowFragments error: nil]; 92 93 DDLog (@ "% @", dic ); 94 95 block (dic ); 96 /************************************** * 97 determine whether to call errorBlock (dic) If there is errorCode 98 in dic) if there is no errorCode, call block (dic100 ******************************/101 102} failure: ^ (AFHTTPRequestOperation * operation, NSError * error) {103 failureBlock (); 104}]; 105 106 op. responseSerializer = [AFHTTPResponseSerializer serializer]; 107 108 109 [op start]; 110 111} 112 113 114 115 @ end 3. config: Create a pch file and Config. h文 pch file introduces common header files with the following content: 1 // 2 // PrefixHeader. pch 3 // MVVMTest 4 // 5 // Created by Li zeru on 15/1/6. 6 // Copyright (c) 2015 Li zeru. all rights reserved. 7 // 8 9 # ifndef MVVMTest_PrefixHeader_pch10 # define MVVMTest_PrefixHeader_pch11 12 # import "AFNetworking. h "13 # import" UIKit + AFNetworking. h "14 # import" Config. h "15 16 # import" NetRequestClass. h "17 # import" SVProgressHUD. h "18 # endif Config. h contains various macro definitions, enumeration types, and block types. The Code is as follows: 1 // 2 // Config. h 3 // MVVMTest 4 // 5 // Created by Li zeru on 15/1/6. 6 // Copyright (c) 2015 Li zeru. all rights reserved. 7 // 8 9 # ifndef # define MVVMTest_Config_h11 12 // define the block type of the returned request data 13 typedef void (^ ReturnValueBlock) (id returnValue); 14 typedef void (^ ErrorCodeBlock) (id errorCode); 15 typedef void (^ FailureBlock) (); 16 typedef void (^ NetWorkBlock) (BOOL netConnetState); 17 18 # define DDLog (xx ,...) NSLog (@ "% s (% d):" xx, _ PRETTY_FUNCTION __, _ LINE __, ##__va_args __) 19 20 // accessToken21 # define ACCESSTOKEN @ "your own access_token" 22 23 // request public microblog Network Interface 24 # define REQUESTPUBLICURL @ www.bkjia.com "25 26 # define SOURCE @" source "27 # define TOKEN @" access_token "28 # define COUNT @" count "29 30 # define STATUSES @" statuses "31 # define CREATETIME @" created_at "32 # define WEIBOID @" id "33 # define WEIBOTEXT @" text "34 # define USER @" user "35 # define UID @" id "36 # define HEADIMAGEURL @" profile_image_url "37 # define USERNAME @" screen_name "38 39 # endif

Related Article

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.