The project uses Afnetwork to put the network layer is also put into the Viewcontroller, data analysis cache These are also in the inside, so the hierarchy may not be clear, today try to separate a bit.
1. Class structure
There are three main parts, top ads, middle icons, and some cells in the lower part of the page. Three classes were designed, Admodel, Iconmodel, Homefeedmodel, and the reference MVVM also designed a viewmodel that declares the service phone and three arrays that store the above class objects. Originally thought that the previous three classes to implement the Nscoding protocol can not think of ViewModel also to achieve.
2.AFNetWorking separation
(1) Problems encountered
Want to write a method can get the request data at the same time can also return the error message, so began to write the following code, run to find MSG, errmsg are empty, after the breakpoint found Afnetwork request part is asynchronous, return before the MSG assignment, let me very tangled, This will have no return value. add __block to modify external variables in the other block
(2) solve
Because of the above problems, two classes of the value of the problem, belong to have a number of return values, asked the next small partner, said the structure has a problem, need to use proxy or block, so I added a block, this next test is possible @Class forward statement
#import <foundation/foundation.h> @class homemodel;typedef void (^ Homemodelblock) (Homemodel *model,nsstring *errmsg); @interface Homemodel:nsobject@property (nonatomic,strong) nsstring *telphone; @property (nonatomic,strong) nsmutablearray *adlist; @property ( Nonatomic,strong) Nsmutablearray *iconlist; @property (Nonatomic,strong) Nsmutablearray *homefeedlist; @property (nonatomic,assign) Homemodelblock Homeblock; -(void @end
////HOMEMODEL.M//XQB////Created by City--online on 15/6/24.////#import "HomeModel.h"#import "AdModel.h"#import "IconModel.h"#import "HomeFeed.h"#import "Global.h"@implementationHomemodel//nscoding Protocol-(void) Encodewithcoder: (Nscoder *) acoder{[Acoder encodeObject:self.telPhone forkey:@"Telphone"]; [Acoder encodeObject:self.adList Forkey:@"adlist"]; [Acoder encodeObject:self.iconList Forkey:@"iconlist"]; [Acoder encodeObject:self.homeFeedList Forkey:@"homefeedlist"];}-(Instancetype) Initwithcoder: (Nscoder *) coder{ Self=[Super Init]; if(self) {_telphone=[coder Decodeobjectforkey:@"Telphone"]; _adlist=[coder Decodeobjectforkey:@"adlist"]; _iconlist=[coder Decodeobjectforkey:@"iconlist"]; _homefeedlist=[coder Decodeobjectforkey:@"homefeedlist"]; } returnSelf ;}-(instancetype) init{ Self=[Super Init]; if(self) {self.adlist=[[Nsmutablearray alloc]init]; Self.iconlist=[[Nsmutablearray alloc]init]; Self.homefeedlist=[[Nsmutablearray alloc]init]; } returnSelf ;}//Network request fetch data-(void) gethomedata{__block Homemodel*homemodel=[[Homemodel alloc]init]; __block NSString*msg=[[NSString Alloc]init]; Afhttprequestoperationmanager*manager =[Afhttprequestoperationmanager Sharemanager]; Nsmutabledictionary*parameters =[[Nsmutabledictionary alloc] initwithdictionary:[commonparameters Getcommonparameters]]; [Parameters Addsignaturekey]; [Manager Get:api_home_url parameters:parameters success:^ (Afhttprequestoperation *operation,IDresponseobject) { //Xqblog (@ "\nresponseobject:%@", responseobject); if([[[Responseobject Objectforkey:xqb_network_error_code] isequaltostring:xqb_network_error_code_ok]) {NSMut Abledictionary*datadic = [Responseobject objectforkey:@"Data"]; Homemodel=[self transdata:datadic]; Msg=@"Chenggong"; _homeblock (Homemodel,nil); } Else { //Load Server Exception interfaceHomemodel=[self unarchiverdata]; Msg=@"Server Exception"; _homeblock (HOMEMODEL,MSG); }}failure:^ (Afhttprequestoperation *operation, Nserror *error) { //Load Network Exception interfacemsg=@"Network Exceptions"; Homemodel=[self unarchiverdata]; _homeblock (HOMEMODEL,MSG); }]; return ;}-(Homemodel *) Transdata: (Nsdictionary *) dic{Homemodel*homemodel=[[Homemodel alloc]init]; NSString*telphone = Dealwithjsonvalue ([dic Objectforkey:@"Telephone"]); Homemodel.telphone=Telphone; Nsarray*adarray = [dic objectforkey:@"Ads"]; for(Nsdictionary *adinfoinchAdarray) {Admodel*admodel =[[Admodel alloc] init]; Admodel.adid= [[Adinfo objectforkey:@"adId"] stringvalue]; Admodel.imageurl= Dealwithjsonvalue ([adinfo Objectforkey:@"Image"]); Admodel.link= Dealwithjsonvalue ([adinfo Objectforkey:@"Link"]); Admodel.title= Dealwithjsonvalue ([adinfo Objectforkey:@"title"]); [Homemodel.adlist Addobject:admodel]; } Nsarray*homeicons = [dic objectforkey:@"homeicons"]; for(Nsdictionary *homeicondicinchhomeicons) {Iconmodel*item =[[Iconmodel alloc] init]; Item.title= [Homeicondic Objectforkey:@"title"]; Item.icon= [Homeicondic Objectforkey:@"icon"]; Item.linkurl= [Homeicondic Objectforkey:@"Linkurl"]; Item.type= [Homeicondic Objectforkey:@"type"]; Item.createid= [[Homeicondic objectforkey:@"Createid"] stringvalue]; Item.iconid= [[Homeicondic objectforkey:@"ID"] stringvalue]; Item.category= [Homeicondic Objectforkey:@"category"]; [Homemodel.iconlist Addobject:item]; } Nsarray*feedsdic = [dic objectforkey:@"Feeds"]; for(Nsdictionary *feeddicinchfeedsdic) {Homefeed*model =[[Homefeed alloc] init]; Model.feedid= [[Feeddic objectforkey:@"Feedid"] stringvalue]; Model.feedtype= [Feeddic Objectforkey:@"Feedtype"]; Model.title= Dealwithjsonvalue ([feeddic Objectforkey:@"title"]); Model.icon= Dealwithjsonvalue ([feeddic Objectforkey:@"icon"]); Model.content= Dealwithjsonvalue ([feeddic Objectforkey:@"content"]); Model.feedcity= [Feeddic Objectforkey:@"feedcity"]; Model.linktype= [Feeddic Objectforkey:@"Linktype"]; [Homemodel.homefeedlist Addobject:model]; } [self Savefile:homemodel]; returnHomemodel;}//solution file-(Homemodel *) unarchiverdata{Nsmutabledata*mutabledata=[[Nsmutabledata alloc]initwithcontentsoffile:[self FilePath]]; Nskeyedunarchiver*unarchiver=[[Nskeyedunarchiver Alloc]initforreadingwithdata:mutabledata]; Homemodel*model=[unarchiver Decodeobjectforkey:@"Homemodel"];//NSLog (@ "%@%@", model.telphone,model.adlist);//For (Iconmodel *admodel in model.iconlist) {//NSLog (@ "%@", admodel.title);// } returnmodel;}//Archive-(void) SaveFile: (Homemodel *) model{Nsmutabledata*mutabledata=[[Nsmutabledata alloc]init]; Nskeyedarchiver*archiver=[[Nskeyedarchiver Alloc]initforwritingwithmutabledata:mutabledata]; [Archiver Encodeobject:model Forkey:@"Homemodel"]; [Archiver finishencoding]; [Mutabledata writetofile:[self FilePath] atomically:yes]; }//Get file path-(NSString *) filepath{Nsarray*documents=nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); NSString*documentpath=[Documents Firstobject]; NSString*filepath=[documentpath stringbyappendingpathcomponent:@"Home"]; returnFilePath;}@end
This will enable the separation of network and Viewcontroller, and then the business logic and network layer can be separated, the project is not small, there is no subdivision
Network separation of IOS projects