1. When to use the cache in the program
2. Caching mechanisms
1) The first time the data is requested, there is no data in the memory cache and no data in the hard disk cache.
2) When the server returns data, you need to do a step
1> using the server's data
2> caching the server's data to the hard disk (sandbox)
At this point, there is data in the memory cache, no data in the hard disk cache
3) The request data is divided into two cases:
1> If the program does not shut down and is running,
Then there is data in the memory cache, and there is data in the hard disk cache. If you request data again at this point, use the data in the memory cache directly.
2> if the program restarts
Then the memory cache data has disappeared, the hard disk cache still exists, if you request data at this time, directly using the hard disk cache data.
Tip: After reading the data from the hard disk cache, there is data in the memory cache.
3. Several common methods of caching and how to use them
1) Archive 2) Generate plist 3) Nsuserdefault 4) SQLite
1.1> Archive: Can store custom objects <NSCoding>
Common methods://Save [Nskeyedarchiver archiverootobject:p Tofile:path];
Take [Nskeyedunarchiver Unarchiverobjectwithfile:path];
/* This method is called when a custom object is saved to a file, where you can describe how to store the properties of the custom object */
-(void) Encodewithcoder: (Nscoder *) Acoder
/* Callback with this method when reading an object in the file, which shows how to read the object stored in the file */
-(ID) Initwithcoder: (Nscoder *) Adecoder
1 #import <Foundation/Foundation.h>23@interface person:nsobject< Nscoding>45 @property (nonatomic, copy) NSString *name; 6 @property (nonatomic, assign) Nsinteger age; 7 @property (nonatomic, assign) BOOL sex; 8 9 @end
#import "Person.h"@implementation Person//Archive- (void) Encodewithcoder: (Nscoder *) acoder{[Acoder encodeObject:self.name forkey:@"name"]; [Acoder encodeInteger:self.age Forkey:@" Age"]; [Acoder encodeBool:self.sex Forkey:@"Sex"];}//solution file-(Instancetype) Initwithcoder: (Nscoder *) adecoder{if(Self = =[Super Init]) {Self.name= [Adecoder Decodeobjectforkey:@"name"]; Self.age= [Adecoder Decodeintegerforkey:@" Age"]; Self.sex= [Adecoder Decodeboolforkey:@"Sex"]; } returnSelf ;}@end
#import "ViewController.h"#import "Person.h"@interfaceViewcontroller () @property (nonatomic, strong) person*Person ;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; [Self savecachedata];}-(Person *) person{if(_person = =Nil) {_person=[[Person alloc] init]; _person.name=@"xinjinying"; _person.age= One; _person.sex=YES; } return_person;}#pragmaMark-Archive Storage cache data-(void) savecachedata{NSString*path =[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject]; NSString*cachepath = [path stringbyappendingpathcomponent:@"Tooyoung.toosimple"]; NSLog (@"%@", CachePath); [Nskeyedarchiver ArchiveRootObject:self.person Tofile:cachepath];}- (void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{nsstring*path =[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject]; NSString*readdatapath = [path stringbyappendingpathcomponent:@"Tooyoung.toosimple"]; NSLog (@"%@", Readdatapath); person*person =[Nskeyedunarchiver Unarchiveobjectwithfile:readdatapath]; NSLog (@"%@-%zd-%d", Person.name,person.age,person.sex); }
2.1> Generating plist files
#pragmaMark-Write Read plist file-(void) writeandreadplistfile{//Read PlistNSString *plistpath = [[NSBundle mainbundle] Pathforresource:@"Plistdemo"OfType:@"plist"]; Nsmutabledictionary*data =[[Nsmutabledictionary alloc] initwithcontentsoffile:plistpath]; //add a piece of content[Data setobject:@"Yoyoyo"Forkey:@"Key"]; //Deposit Yoyoyo.plistNSString *path =[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject]; NSString*writepath = [path stringbyappendingpathcomponent:@"yoyoyo.plist"]; [Data Writetofile:writepath Atomically:yes]; }
3.1>nsuserdefault (commonly used in the project to save user name, password, Accesstoken, version number ...) )
1 #pragmaMark-nsuserdefaults2- (void) Userdefaultcache3 {4 //Save5Nsuserdefaults *userdefaults =[Nsuserdefaults standarduserdefaults];6[Userdefaults SetObject:@"Fuck"Forkey:@"name"];7 [Userdefaults synchronize];8 }9- (void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)EventTen { One //Take ANsuserdefaults *userdetaults =[Nsuserdefaults standarduserdefaults]; -NSString *name = [Userdetaults objectforkey:@"name"]; -NSLog (@"%@", name); the}
Cond...
iOS Cache Series---1.0