zoukankan      html  css  js  c++  java
  • iOS缓存


         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 - 归档存储缓存数据
    - (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 *)event
    10 {
    11     //12     NSUserDefaults *userDetaults = [NSUserDefaults standardUserDefaults];
    13     NSString *name = [userDetaults objectForKey:@"name"];
    14     NSLog(@"%@",name);
    15 }    
  • 相关阅读:
    JVM 综述
    看 Netty 在 Dubbo 中如何应用
    Netty 心跳服务之 IdleStateHandler 源码分析
    Netty 高性能之道
    Netty 解码器抽象父类 ByteToMessageDecoder 源码解析
    Netty 源码剖析之 unSafe.write 方法
    Netty 出站缓冲区 ChannelOutboundBuffer 源码解析(isWritable 属性的重要性)
    Netty 源码剖析之 unSafe.read 方法
    Netty 内存回收之 noCleaner 策略
    Netty 源码阅读的思考------耗时业务到底该如何处理
  • 原文地址:https://www.cnblogs.com/MyBlogZH/p/5529988.html
Copyright © 2011-2022 走看看