ios离线读取上次网络请求的数据。

记录下自己学的东西。


请求的是一个公告栏,有公告的title,发布的日期,还有公告详情的url(便于下次请求查看公告详情)

noticeTitle,noticeDate,noticeUrl  都是数组


一,首先判断沙盒里面是否含有我们前一次请求存下的数据。

- (void)viewDidLoad

{

........

//取沙盒里的数据

    NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

    NSString *noticeTitlePath = [[pathsobjectAtIndex:0]stringByAppendingPathComponent:@"noticeTitle.txt"];

    NSString *noticeDatePath = [[pathsobjectAtIndex:0]stringByAppendingPathComponent:@"noticeDate.txt"];

    NSString *noticeUrlPath = [[pathsobjectAtIndex:0]stringByAppendingPathComponent:@"noticeUrl.txt"];

    

    _noticeTitle = [[NSMutableArrayalloc] initWithContentsOfFile:noticeTitlePath];

    _noticeDate = [[NSMutableArrayalloc] initWithContentsOfFile:noticeDatePath];

    _noticeUrl = [[NSMutableArrayalloc] initWithContentsOfFile:noticeUrlPath];

    

    if (!_noticeTitle) {

        [SVProgressHUDshowWithStatus:@"加载中..."maskType:SVProgressHUDMaskTypeClear];

        [NSThreaddetachNewThreadSelector:@selector(setLoadData)toTarget:selfwithObject:nil];

        //如果没有,线程去请求数据。setLoadData方法是请求数据的方法。

    }

}


二,在请求数据完成后,存储到沙盒中,如果是第二次存储,文件会自动替换。

NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

    NSString *noticeTitlePath = [[pathsobjectAtIndex:0]stringByAppendingPathComponent:@"noticeTitle.txt"];

    NSString *noticeDatePath = [[pathsobjectAtIndex:0]stringByAppendingPathComponent:@"noticeDate.txt"];

    NSString *noticeUrlPath = [[pathsobjectAtIndex:0]stringByAppendingPathComponent:@"noticeUrl.txt"];

    

    [self.noticeTitlewriteToFile:noticeTitlePath atomically:YES];

    [self.noticeDatewriteToFile:noticeDatePath atomically:YES];

    [self.noticeUrlwriteToFile:noticeUrlPath atomically:YES];

//这三个数组是数据,获取返回信息,解析后加入进数组,写入沙盒。


三,用户手动更新公告栏,再次调用

[NSThreaddetachNewThreadSelector:@selector(setLoadData)toTarget:selfwithObject:nil];

这样,沙盒里面的数据就是最新的公告栏信息了。(新的信息会自动替换就的信息)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值