离线缓存

之前的项目因为实时性要求比较高,所以一打开客户端,就开始做网络请求.现在想想,是没有做内容的离线缓存.这个问题,我没意识到.产品经理也没有意识到...

我觉得Archiver,来做比较合适,可复写.可直接从存储中读取model,(当然要在相应的model里实现NSCoding协议)代码如下

#pragma mark ============实现NSCoding协议 

//归档
- (void)encodeWithCoder:(NSCoder *)Coder
{ NSDate * senddate=[NSDate date]; NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init]; [dateformatter setDateFormat:@"YYYY-MM-dd,hh:mm:ss"]; NSString *str = [NSString stringWithFormat:@"Cached@:%@",[dateformatter stringFromDate:senddate]]; [Coder encodeObject:_citynm forKey:@"CityName"];
[Coder encodeObject:_weather forKey:@"Weather"];
[Coder encodeObject:_temperature_curr forKey:@"NowTemp"];
[Coder encodeObject:str forKey:@"days"];
[Coder encodeObject:UIImagePNGRepresentation(_Logo) forKey:@"img"]; } //解档
- (nullable instancetype)initWithCoder:(NSCoder *)Decoder // NS_DESIGNATED_INITIALIZER
{ if (self = [super init]) { self.citynm = [Decoder decodeObjectForKey:@"CityName"];
self.weather = [Decoder decodeObjectForKey:@"Weather"];
self.temperature_curr = [Decoder decodeObjectForKey:@"NowTemp"];
self.days = [Decoder decodeObjectForKey:@"days"];
self.Logo = [UIImage imageWithData:[Decoder decodeObjectForKey:@"img"]]; } return self;
}

Archiver的封装

#import <Foundation/Foundation.h>

@interface ArchiverCache : NSObject

/**
* 归档
*
* @param model model
* @param Key Key
*/
-(void)EncoderDoWithModel:(id)model withKey:(NSString*)Key; /**
* 反归档
*
* @param Key
*
* @return (id)Model
*/
-(id)UncoderDoWith:(NSString*)Key; @end ====================.M================= #import "ArchiverCache.h" @implementation ArchiverCache //archiver
-(void)EncoderDoWithModel:(id)model withKey:(NSString *)Key
{
NSMutableData *data = [[NSMutableData alloc] init]; //创建归档辅助类
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; //编码
[archiver encodeObject:model forKey:Key];
//结束编码
[archiver finishEncoding];
//写入 if ([data writeToFile:[self GetFilePath] atomically:YES]) { NSLog(@"Cache Set Success"); }else{ NSLog(@"Cache Set Fail"); } } //Unarchiver
-(id)UncoderDoWith:(NSString *)Key
{
///////////////////////解档
NSData *_data = [[NSData alloc] initWithContentsOfFile:[self GetFilePath]];
//解档辅助
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:_data]; //解码并解档出model
id Wm = [unarchiver decodeObjectForKey:Key];
//关闭解档
[unarchiver finishDecoding]; return Wm;
} -(NSString *)GetFilePath
{
NSArray *arr = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
//虽然该方法返回的是一个数组,但是由于一个目标文件夹只有一个目录,所以数组中只有一个元素。
NSString *cachePath = [arr lastObject]; NSString *filePath = [cachePath stringByAppendingPathComponent:@"Model"];
// NSLog(@"%@",filePath); return filePath;
}
@end

调用,在这个例子里,每次进行model操作的时候,都会直接进行归档操作.这样可以保持归档里的数据都是最新的.

-(id)NetViewModelWithCache
{
//读取缓存...
ArchiverCache *ar = [[ArchiverCache alloc] init];
//需在相应的model实现 initWithCoder;
WeatherModel *Wm = [ar UncoderDoWith:@"weather"]; return Wm;
} -(id)ConvertToModel:(id)Data
{ WeatherModel *model = [[WeatherModel alloc] initWithDictionary:Data]; ///转成模型,同时把IMG给下载了.....由于API里的ICON是GIF,为了省事,这里直接另找了一张图.(可用sdwebimage来处理gif)
NSString *imageURLStr = @"http://pic.58pic.com/58pic/15/48/73/04f58PIC37y_1024.png"; NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imageURLStr]]; model.Logo = [UIImage imageWithData:imageData]; ArchiverCache *ar = [[ArchiverCache alloc] init]; //需在相应的model实现 encodeWithCoder;
[ar EncoderDoWithModel:model withKey:@"weather"]; return model;
}

上个项目的一些反思 III的更多相关文章

  1. 上个项目的一些反思 II

    上个项目需要使用通讯录,我在回顾自己设计的时候,发现自己少设计了cache这一环. 虽然直接用SQLite在初期体验上没什么大损失,不过可以预想通讯录增长到一定数量后势必会影响体验. 单例模式,全局缓 ...

  2. 上个项目的一些反思 I

    最近一直在反思之前的项目,发现了很多问题.比如数据安全... 虽然项目需求是只展示最新的数据,所以几乎没用什么本地存储.除了通讯录和用户的Token. 用户通讯录另表,今天反思下用户的Token的存储 ...

  3. git上传项目代码到github

    参考: git学习——上传项目代码到github github上传时出现error: src refspec master does not match any解决办法 git 上传本地文件到gith ...

  4. GitHub的用法:到GitHub上部署项目

    先提供两个较好的Git教程: 1. 如何在github部署项目: lhttp://jingyan.baidu.com/article/656db918fbf70ce381249c15.html 2. ...

  5. 如何从eclipse中下载并导入Github上的项目

    eclipse导入项目,方法就是点击File ->Import,选择Existing Projects into Workspace 但前提是,你导入的这个项目原本就是用eclipse的构建的, ...

  6. 参与github上开源项目的大致流程和注意事项

    Foreword github是一个很火的代码托管服务网站,可能好多人都想参与一两个项目玩一玩学习一下,但由于是纯英文的网站,可能又会止步于想法上没有动手实践.接下来我就介绍一下参与github上开源 ...

  7. 在GitHub上管理项目

    在GitHub上管理项目 新建repository 本地目录下,在命令行里新建一个代码仓库(repository) 里面只有一个README.md 命令如下: touch README.md git ...

  8. Eclipse-将svn上的项目转化成相应的项目

    这里假设svn上的项目为maven项目 首先从svn检出项目 其中项目名称code可自己定义更改新的名称 从svn检出的项目结构 然后将项目转化成相关的项目 转换加载中 加载/下载 maven相关内容 ...

  9. 利用gitbash上传项目到github

    GitHub主要是用作基于Git的分布式版本管理系统的库,可以保存和管理自己的代码,而且主要用作代码的合作开发.不过对于我来说,Git控制系统还比较难以掌握,或者开发小系统还不太用得着,因此我把Git ...

随机推荐

  1. [LeetCode] Bitwise AND of Numbers Range 数字范围位相与

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  2. [LeetCode] Remove Element 移除元素

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  3. hihocoder-1142-三分求极值

    Hihocoder-1142 : 三分·三分求极值 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 这一次我们就简单一点了,题目在此: 在直角坐标系中有一条抛物线y=ax ...

  4. 初步学习border-radius

    1.属性解析 border-radius是css3属性,他可以使div的角进行一定程度的弯曲. 比如说下面这个width和height的正方形div 经过设置border-radius之后四个角会出现 ...

  5. BootStrap table使用

    bootstrap table git address https://github.com/wenzhixin/bootstrap-table 引入文件 <link rel="sty ...

  6. [原创]vscode初体验

    这段时间,在网上看见很多从.net转java的,为什么会造成这样的情况,我感觉有几点 1.  微软在中国的生态不好,死要钱,很多公司都不想花这部分钱 2.  做.net开发人,工资普遍较低 前言 闲聊 ...

  7. 怎么在MindManager中查看打印预览

    在MindManager2016思维导图中打印导图之前,可以先进行预览,MindManager和其他很多应用程序一样都带有打印预览功能,该功能提供了再次检查的机会,避免打印出错,MindManager ...

  8. 【转】Tomcat启用HTTPS协议配置过程

    转载请注明出处: http://blog.csdn.net/gane_cheng/article/details/53001846 http://www.ganecheng.tech/blog/530 ...

  9. JNI开发的常见错误

    1. 写错了load的library java.lang.UnsatisfiedLinkError: Couldn't load hell0: findLibrary returned null 2. ...

  10. Python matplotlib笔记

    可视化的工具有很多,如Tableau,各种JS框架,我个人感觉应该是学JS最好,因为JS不需要环境,每个电脑都有浏览器,而像matplotlib需要Python这样的开发环境,还是比较麻烦的,但是毕竟 ...