Some reflections on the previous project III

Source: Internet
Author: User

Offline caching

The previous project because of the real-time requirements are relatively high, so that when the client opens, it begins to make network requests. Now think about it, there is no offline cache to do the content. The problem, I didn't realize. The product manager didn't realize ...

I think archiver, to do more appropriate, can be replicated. You can read the model directly from the store (and, of course, implement the Nscoding protocol in the corresponding model) code as follows

#pragmaMark ============ implements Nscoding protocol//Archive- (void) Encodewithcoder: (Nscoder *) coder{NSDate* senddate=[NSDate Date]; NSDateFormatter*dateformatter=[[NSDateFormatter alloc] init]; [Dateformatter Setdateformat:@"Yyyy-mm-dd,hh:mm:ss"]; NSString*STR = [NSString stringWithFormat:@"[Email protected]:%@", [Dateformatter Stringfromdate:senddate]]; [Coder encodeobject:_citynm Forkey:@"CityName"]; [Coder Encodeobject:_weather Forkey:@"Weather"]; [Coder Encodeobject:_temperature_curr Forkey:@"nowtemp"]; [Coder encodeobject:str Forkey:@" Days"]; [Coder Encodeobject:uiimagepngrepresentation (_logo) Forkey:@"img"]; }//solution file-(Nullable Instancetype) Initwithcoder: (Nscoder *) Decoder//Ns_designated_initializer{        if(self =[Super Init]) {self.citynm= [Decoder Decodeobjectforkey:@"CityName"]; Self.weather= [Decoder Decodeobjectforkey:@"Weather"]; Self.temperature_curr= [Decoder Decodeobjectforkey:@"nowtemp"]; Self.days= [Decoder Decodeobjectforkey:@" Days"]; Self. Logo= [UIImage imagewithdata:[decoder decodeobjectforkey:@"img"]]; }        returnSelf ;}

Package of Archiver

#import<Foundation/Foundation.h>@interfaceArchivercache:nsobject/** * Archive * * @param model model* @param key key*/-(void) Encoderdowithmodel: (ID) Model Withkey: (nsstring*) Key;/** * Anti-archive * * @param Key * * @return (ID) Model*/-(ID) Uncoderdowith: (nsstring*) Key;@end====================. m=================#import "ArchiverCache.h"@implementationArchivercache//archiver-(void) Encoderdowithmodel: (ID) Model Withkey: (NSString *) key{Nsmutabledata*data =[[Nsmutabledata alloc] init]; //Create an archive helper classNskeyedarchiver *archiver =[[Nskeyedarchiver alloc] initforwritingwithmutabledata:data]; //Coding[Archiver Encodeobject:model Forkey:key]; //End Encoding[Archiver finishencoding]; //Write        if([Data writetofile:[self GetFilePath] atomically:yes]) {NSLog (@"Cache Set Success"); }Else{NSLog (@"Cache Set Fail"); }    }//Unarchiver-(ID) Uncoderdowith: (NSString *) key{///////////////////////Solve filesNSData *_data =[[NSData alloc] initwithcontentsoffile:[self GetFilePath]]; //Solution AssistNskeyedunarchiver *unarchiver =[[Nskeyedunarchiver alloc] initforreadingwithdata:_data]; //decode and de-profile the model    IDWm =[Unarchiver Decodeobjectforkey:key]; //Close the solution[Unarchiver finishdecoding]; returnWm;}-(NSString *) getfilepath{Nsarray*arr =nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES); //Although the method returns an array, there is only one element in the array because a destination folder has only one directory. NSString *cachepath =[arr Lastobject]; NSString*filepath = [CachePath stringbyappendingpathcomponent:@"Model"];//NSLog (@ "%@", FilePath);        returnFilePath;}@end

Call, in this case, each time the model operation, it will be directly archived operations. This keeps the data in the archive up to date.

-(ID) netviewmodelwithcache{//Read Cache ...Archivercache *ar =[[Archivercache alloc] init]; //It is necessary to implement Initwithcoder in the corresponding model;Weathermodel *wm = [ar uncoderdowith:@"Weather"]; returnWm;}-(ID) Converttomodel: (ID) data{Weathermodel*model =[[Weathermodel alloc] initwithdictionary:data]; ///into the model, while the img to download ..... Because the API icon is GIF, in order to save trouble, here directly to find a picture. (Use Sdwebimage to process gif)NSString *imageurlstr =@"Http://pic.58pic.com/58pic/15/48/73/04f58PIC37y_1024.png"; NSData*imagedata =[[NSData alloc] Initwithcontentsofurl:[nsurl Urlwithstring:imageurlstr]]; Model. Logo=[UIImage Imagewithdata:imagedata]; Archivercache*ar =[[Archivercache alloc] init]; //It is necessary to implement Encodewithcoder in the corresponding model;[Ar encoderdowithmodel:model Withkey:@"Weather"]; returnmodel;}

Some reflections on the previous project III

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.