【Object-C】简单实现plist离线缓存数据

iOS---数据离线缓存

离线缓存

为了用户的体验,不需要每次打开App都加载新数据,或者重新请求数据,因此需要把每次浏览的数据保存起来,当下次打开软件时,首先从沙盒中加载数据;或者当软件未联网时,也只能从沙盒中加载旧数据。

离线数据的方法选择

1.plist文件2.Document路径3.数据库

由于保存的是大批量数据,且会不停的刷新新数据,因此应该选择数据库来存储。

离线缓存的思路

当第一次打开应用程序时,把界面加载好的数据保存到沙盒中

当下一次进入应用程序时,首先从沙盒中找

如果没有网络,直接加载上次保存的数据,或者没有比较新的数据也从沙盒中加载数据。

= =  =  =  = = =分 = = = = = = = = = =割 = = =  = = = = = = =线 = = = = = = = = = = =  =

本文介绍下缓存plist的项目用法:

Plist缓存一般是一次性缓存的,轻量级的数据都可以存放Plist,以xml的方式存储沙盒本地,读取效率好

直接上代码:

(1)、简单创建plist归档的工具类


归档、反归档工具

#program Mark  ==.h 文件 代码

#import

@interfaceASArchiverTools :NSObject

//归档的工具方法

+ (void)archiverObject:(id)object ByKey:(NSString*)key

WithPath:(NSString*)path;

+ (id)unarchiverObjectByKey:(NSString*)key

WithPath:(NSString*)path;

@end

说明:方法中的key是存储文件的一个标识!!!!

#program Mark  ==.m 文件代码

//归档

+ (void)archiverObject:(id)object ByKey:(NSString*)key WithPath:(NSString*)path

{

//初始化存储对象信息的data

NSMutableData*data = [NSMutableDatadata];

//创建归档工具对象

NSKeyedArchiver*archiver = [[NSKeyedArchiveralloc]initForWritingWithMutableData:data];

//开始归档

[archiverencodeObject:objectforKey:key];

//结束归档

[archiverfinishEncoding];

//写入本地

NSString*docPath =NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES).lastObject;

NSString*destPath = [[docPathstringByAppendingPathComponent:@"Caches"]stringByAppendingPathComponent:path];

NSLog(@"destPath == %@",destPath);

[datawriteToFile:destPathatomically:YES];

}

//反归档

+ (id)unarchiverObjectByKey:(NSString*)key WithPath:(NSString*)path

{

NSString*docPath =NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES).lastObject;

NSString*destPath = [[docPathstringByAppendingPathComponent:@"Caches"]stringByAppendingPathComponent:path];

NSLog(@"destPath == %@",destPath);

NSData*data = [NSDatadataWithContentsOfFile:destPath];

//创建反归档对象

NSKeyedUnarchiver*unarchiver = [[NSKeyedUnarchiveralloc]initForReadingWithData:data];

//接收反归档得到的对象

idobject = [unarchiverdecodeObjectForKey:key];

returnobject;

}

(2)、在网络请求的地方实现将数据写入plist缓存

//获取本地沙盒路径

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

//获取完整路径

NSString*documentsPath = [pathobjectAtIndex:0];

NSString*plistPath = [documentsPathstringByAppendingPathComponent:@"groups.plist"];

NSLog(@"plistPath == %@",plistPath);

//设置属性值

//写入文件

[self.friendsArraywriteToFile:plistPathatomically:YES];


实现效果
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 前言 iOS本地缓存数据方式有五种: 1.直接写文件方式:可以存储的对象有NSString、NSArray、NSD...
    GitHubPorter阅读 19,362评论 1 13
  • iOS本地缓存数据方式有五种: 1.直接写文件方式:可以存储的对象有NSString、NSArray、NSDict...
    爱哼的阿狸阅读 3,688评论 0 0
  • 前言iOS本地缓存数据方式有五种:1.直接写文件方式:可以存储的对象有NSString、NSArray、NSDic...
    LZM轮回阅读 4,035评论 0 2
  • 沙盒 iOS中的沙盒机制是一种安全体系。每个应用程序在安装时,会创建属于自己的沙盒文件(存储空间)。应用...
    gpylove阅读 2,087评论 0 0
  • 娃4岁时是一个十分值得家长珍惜呵护的年龄。 娃4岁时有着旺盛的求知欲,对任何东西都很好奇,不仅表现在对各种绘本、电...
    corlorwind阅读 2,924评论 1 0

友情链接更多精彩内容