ios中 对网络请求数据文件本地缓存
2012-09-16 10:46
489 查看
之前对ios缓存机制不了解,期间走了很多弯路。以至于在原来公司的第一个项目没有能够提供缓存机制。全部数据都是实时的网络数据。后来经过自己向周围有经验的ios开发人员请教,了解到NSCoder类,以及NSCoding协议。
原来的思路是把本地下载下来的数据写道sqllite数据库中,但是以后用的时间没有办法确定写在数据库中的文件在界面上应该显示的位置。被这个问题纠结了好长时间。周围ios开发人员对缓存又不太熟悉。直到来到现在的公司才有合适的人给解决了这个问题。其实思路并不是很难。
1.首先,对下载下来的数据(json或xml)经过自己的解析,一般是存为NSArray或者NSDictionary的数据源对象。其中数据源中的每个对象(model)都要实现NSCoding协议。
Example:我们的项目中有一个表示活动的model类。其中有getter和setter方法,代码就不贴出来了。其中有userid,activityid,messageCount,memberCount等属性。在实现NSCodeing协议后。有两个协议方法要在model类中实现。代码如下:
#pragma mark – NSCoding
//keyfor value的key
不贴出
- (void)encodeWithCoder:(NSCoder*)aCoder {
[aCoder encodeInt64:self.userIdforKey:1];
[aCoder encodeInt64:self.activityidforKey:2];
[aCoder encodeInteger:self.newmessagecountforKey:3];
[aCoder encodeInteger:self.newmembercountforKey:4];
}
- (id)initWithCoder:(NSCoder*)aDecoder {
self =[superinit];
if (self) {
self.userId
= [aDecoder decodeInt64ForKey:1];
self.activityid =[aDecoder
decodeInt64ForKey:2];
self.newmessagecount =[aDecoder
decodeIntegerForKey:3];
self.newmembercount =[aDecoder
decodeIntegerForKey:4];
}
returnself;
}
注意:如果某个model中有NSMutableArray或NSMutableDictionary的属性,属性中如果存的不是向int,double,NSString等基本类型,也就是说,model中某个属性是其他model或者其他model的集合,则这些子model也要实现NSCoding协议。直到这个链中的属性都被基本类型位置。至于可以编码解码的函数可以去NSCoder.h中去找。
2.这样最重要的一步已经完成,然后就可以把网络请求返回的数据源存在本地。
//指定沙盒文件路径[FileOperDocumentFilePath:fileName].不用判断文件是否存在,同名会覆盖。
- (void)saveMyActionData:(NSMutableArray *)fileArraywithFileName:(NSString*)fileName
{
if(fileArray ==
nil) {
return;
}
NSMutableData*data = [[NSMutableDataalloc]
init];
NSKeyedArchiver*archiver = [[NSKeyedArchiveralloc]
initForWritingWithMutableData:data];
[archiverencodeObject:fileArray
forKey:fileName];
[archiverfinishEncoding];
[data writeToFile: [FileOperDocumentFilePath:fileName]atomically:YES];
[archiverrelease];
[data release];
}
3.然后在用的时间从沙盒中把缓存文件load出来。
- (NSMutableArray *)loadMyActionData:(NSString *)fileName
{
NSMutableArray *actionArray =
nil;
NSData*data = [[NSMutableDataalloc]
initWithContentsOfFile: [FileOperDocumentFilePath:fileName]];
if(data) {
NSKeyedUnarchiver*unarchiver = [[NSKeyedUnarchiveralloc]
initForReadingWithData:data];
actionArray = [unarchiver
decodeObjectForKey:fileName];
[unarchiver
release];
}
[data release];
returnactionArray;
}
原来的思路是把本地下载下来的数据写道sqllite数据库中,但是以后用的时间没有办法确定写在数据库中的文件在界面上应该显示的位置。被这个问题纠结了好长时间。周围ios开发人员对缓存又不太熟悉。直到来到现在的公司才有合适的人给解决了这个问题。其实思路并不是很难。
1.首先,对下载下来的数据(json或xml)经过自己的解析,一般是存为NSArray或者NSDictionary的数据源对象。其中数据源中的每个对象(model)都要实现NSCoding协议。
Example:我们的项目中有一个表示活动的model类。其中有getter和setter方法,代码就不贴出来了。其中有userid,activityid,messageCount,memberCount等属性。在实现NSCodeing协议后。有两个协议方法要在model类中实现。代码如下:
#pragma mark – NSCoding
//keyfor value的key
不贴出
- (void)encodeWithCoder:(NSCoder*)aCoder {
[aCoder encodeInt64:self.userIdforKey:1];
[aCoder encodeInt64:self.activityidforKey:2];
[aCoder encodeInteger:self.newmessagecountforKey:3];
[aCoder encodeInteger:self.newmembercountforKey:4];
}
- (id)initWithCoder:(NSCoder*)aDecoder {
self =[superinit];
if (self) {
self.userId
= [aDecoder decodeInt64ForKey:1];
self.activityid =[aDecoder
decodeInt64ForKey:2];
self.newmessagecount =[aDecoder
decodeIntegerForKey:3];
self.newmembercount =[aDecoder
decodeIntegerForKey:4];
}
returnself;
}
注意:如果某个model中有NSMutableArray或NSMutableDictionary的属性,属性中如果存的不是向int,double,NSString等基本类型,也就是说,model中某个属性是其他model或者其他model的集合,则这些子model也要实现NSCoding协议。直到这个链中的属性都被基本类型位置。至于可以编码解码的函数可以去NSCoder.h中去找。
2.这样最重要的一步已经完成,然后就可以把网络请求返回的数据源存在本地。
//指定沙盒文件路径[FileOperDocumentFilePath:fileName].不用判断文件是否存在,同名会覆盖。
- (void)saveMyActionData:(NSMutableArray *)fileArraywithFileName:(NSString*)fileName
{
if(fileArray ==
nil) {
return;
}
NSMutableData*data = [[NSMutableDataalloc]
init];
NSKeyedArchiver*archiver = [[NSKeyedArchiveralloc]
initForWritingWithMutableData:data];
[archiverencodeObject:fileArray
forKey:fileName];
[archiverfinishEncoding];
[data writeToFile: [FileOperDocumentFilePath:fileName]atomically:YES];
[archiverrelease];
[data release];
}
3.然后在用的时间从沙盒中把缓存文件load出来。
- (NSMutableArray *)loadMyActionData:(NSString *)fileName
{
NSMutableArray *actionArray =
nil;
NSData*data = [[NSMutableDataalloc]
initWithContentsOfFile: [FileOperDocumentFilePath:fileName]];
if(data) {
NSKeyedUnarchiver*unarchiver = [[NSKeyedUnarchiveralloc]
initForReadingWithData:data];
actionArray = [unarchiver
decodeObjectForKey:fileName];
[unarchiver
release];
}
[data release];
returnactionArray;
}
相关文章推荐
- ios中使用block回调或代理方式请求网络接口数据(包括网络缓存)
- iOS Http网络请求、缓存、及网络数据更新才请求
- iOS 网络请求数据缓存
- iOS 带缓存,带网络监控的数据请求
- 网络通信中关于请求数据、断点续传和写入本地文件
- Android网络请求缓存,本地数据缓存流程图
- ios网络加载图片进行本地文件缓存开源插件sdwebimagecache
- Android中网络请求数据以及其他数据本地缓存
- iOS基础8:自定义MyData/自定义SQLite用于网络判断,版本判断,图片缓存处理,下载或者上传的GET或POST请求,加密手段,.数据解析
- iOS五种本地缓存数据方式 96 GitHubPorter 2016.05.19 14:04 字数 3917 阅读 4579评论 1喜欢 8 前言 iOS本地缓存数据方式有五种: 1.直接写文件
- 数据以文件形式缓存至本地
- ios获取网络请求失败相关数据
- ios网络学习------4 UIWebView的加载本地数据的三种方式
- IOS网络请求之ASIFormDataRequest 上传数据
- iOS 10 的坑:新机首次安装 app,请求网络权限“是否允许使用数据”
- iOS 网络请求数据两种请求方式GET,POST
- 网络请求框架——OkGo解读(一)——数据的缓存
- Vue2.0 使用vue-resource 模拟网络请求加载本地数据
- iOS开发本地缓存(数据离线缓存、…
- iOS-使用NSURLConnection进行Http Get和Post请求网络数据