ios緩衝系列---1.0,ios緩衝---1.0
1. 程式中什麼時候用到緩衝
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 - Archive Storage快取資料- (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 }
待續...