1. 程序中什么时候用到缓存
images/loading.gif' data-original="webkit-fake-url://f74254b3-1197-4fbc-8157-51aa694c491a/application.pdf" />
2. 缓存机制
1)第一次请求数据时,内存缓存中没有数据,硬盘缓存中没有数据。
2)当服务器返回数据时,需要做一下步骤
1>使用服务器的数据
2>将服务器的数据缓存到硬盘(沙盒)
此时,内存缓存中有数据,硬盘缓存中没有数据
3)再次请求数据分为两种情况:
1>如果程序并没有关闭,一直在运行,
那么此时内存缓存中有数据,硬盘缓存中有数据。如果此时再次请求数据,直接使用内存缓存中的数据即可。
2>如果程序重新启动
那么此时内存缓存的数据已经消失,硬盘缓存依旧存在,如果此时再请求数据,直接使用硬盘缓存的数据即可。
提示:从硬盘缓存中读取数据后,内存缓存中又有数据了。
3.缓存的几种常用方法及具体如何使用
1) 归档
2) 生成plist
3) NSUserDefault
4) sqlite
1.1>归档:可以存放自定义对象 <NSCoding>
常用方法: //存 [NSKeyedArchiver archiveRootObject:p toFile:path];
//取 [NSKeyedUnarchiver unarchiverObjectWithFile:path];
/*当将一个自定义对象保存到文件的时候就会调用该方法,在该方法中说明如何存储自定义对象的属性*/
-(void)encodeWithCoder:(NSCoder *)aCoder
/*当文件中读取一个对象的时候回调用该方法,在该方法中说明如何读取保存在文件中的对象*/
-(id)initWithCoder:(NSCoder *)aDecoder
1 #import <Foundation/Foundation.h>2 3 @interface Person : NSObject<NSCoding>4 5 @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//归档- (void)encodeWithCoder:(NSCoder *)aCoder{ [aCoder encodeObject:self.name forKey:@"name"]; [aCoder encodeInteger:self.age forKey:@"age"]; [aCoder encodeBool:self.sex forKey:@"sex"];}//解档- (instancetype)initWithCoder:(NSCoder *)aDecoder{ if (self == [super init]) { self.name = [aDecoder decodeObjectForKey:@"name"]; self.age = [aDecoder decodeIntegerForKey:@"age"]; self.sex = [aDecoder decodeBoolForKey:@"sex"]; } return self;}@end
#import "ViewController.h"#import "Person.h"@interface ViewController ()@property (nonatomic, strong) Person *person;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; [self saveCacheData];}- (Person *)person{ if (_person == nil) { _person = [[Person alloc] init]; _person.name = @"xinjinying"; _person.age = 11; _person.sex = YES; } return _person;}#pragma mark - 归档存储缓存数据- (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>生成plist文件
#pragma mark - 写入读取plist文件- (void)writeAndReadPlistFile{ //读取plist NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"plistdemo" ofType:@"plist"]; NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath]; //添加一项内容 [data setObject:@"yoyoyo" forKey:@"key"]; //存入yoyoyo.plist NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *writePath = [path stringByAppendingPathComponent:@"yoyoyo.plist"]; [data writeToFile:writePath atomically:YES]; }
3.1>NSUserDefault(项目里一般用来存用户名,密码,accessToken,版本号...)
1 #pragma mark - NSUserdefaults 2 - (void)userDefaultCache 3 { 4 //存 5 NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; 6 [userDefaults setObject:@"fuck" forKey:@"name"]; 7 [userDefaults synchronize]; 8 } 9 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event10 {11 //取12 NSUserDefaults *userDetaults = [NSUserDefaults standardUserDefaults];13 NSString *name = [userDetaults objectForKey:@"name"];14 NSLog(@"%@",name);15 }
待续...
原标题:ios缓存系列
关键词:IOS