ios緩衝系列---1.0,ios緩衝---1.0

來源:互聯網
上載者:User

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 }    

 

待續...

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.