离线缓存

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

我觉得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. Firefox默认英文修改中文

    对于firefox,中文还是看着顺眼,为了自己的顺心.动起手来,自力更生,丰衣足食! 01.确定Linux的firefox版本 firefox -v 02.下载对应版本的中文语言包 http://ft ...

  2. jquery版固定边栏滚动特效

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  3. jQuery 模态对话框示例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. com.panie 项目开发随笔_前后端框架考虑(2016.12.8)

    (一) 近日和一同学联系,说了我想要做一个网站的打算.她很感兴趣.于是我们协商了下,便觉得一起合作.她写前端,我写后台.因为我对于前端样式设计并不怎么熟悉. (二) 我们决定先做一个 个人博客. 网上 ...

  5. Android Studio导入项目慢的问题

    在Github下载的项目,导入studio时非常慢,原因是下载的项目中的gradle与当前gradle不匹配,需要更新包. 解决办法:修改下载包中的文件 1. xxx-project/.idea/gr ...

  6. xv6的课本翻译之——附录B 系统启动器

    Appendix B 附录 B Figure B-1 The relationship between logical, linear, and physical addresses. 图B-1:逻辑 ...

  7. http请求与响应(content-type)

    http请求信息由浏览器把地址栏URL信息和页面(html.jsp.asp)组装成http请求消息体(如下). <request-line>(请求消息行)<headers>(请 ...

  8. 翻书插件:bookblock.js

    BookBlock 是一个 jQuery插件,用来制作带有翻页效果的小书册.可以用任何形式的内容,比如图像或文本.插件会在翻页时利用变形模拟手工翻页,并产生重叠的阴影以达到更逼真的效果. 基本页面 & ...

  9. JKS和PKCS#12

    今天来点实际工作中的硬通货! 与计费系统打交道,少不了用到加密/解密实现.为了安全起见,通过非对称加密交换对称加密密钥更是不可或缺.那么需要通过什么载体传递非对称算法公钥/私钥信息?数字证书是公钥的载 ...

  10. c# .net获取文件夹下的所有文件(多层递归),并获取区间数据(Jsion,xml等数据)写出到处理文件,学习分享~

    static void Main(string[] args)        { string path = string.Format(@"C:\Users\Administrator\D ...