离线缓存

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

我觉得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] Jump Game II 跳跃游戏之二

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  2. 如何在ASP.NET Core中实现CORS跨域

    注:下载本文的完整代码示例请访问 > How to enable CORS(Cross-origin resource sharing) in ASP.NET Core 如何在ASP.NET C ...

  3. C-RAN 集中化、协作化、云化、绿色节能(4C)

    中国移动C-RAN力拼第4个C:2018年6月外场组网验证 http://www.c114.net ( 2016/11/22 07:41 ) C114讯 11月22日早间消息(子月)2009年,中国移 ...

  4. win10使用技巧之如何打出偏僻字母

    一.背景 有时需要在打出一些希腊字母,诸如ɛ.μ等字符,如果输入法不支持该怎么办呢?在很多国产拼音软件中,都会提供扩展方便用户寻找这类字符,但是如果用户换过一款软件,可能要在一定时间找到这些字符就没那 ...

  5. How to Disable Strict SQL Mode in MySQL 5.7

    If your app was written for older versions of MySQL and is not compatible with strict SQL mode in My ...

  6. 如何把Spring制作成jar包,然后在项目里运行。

    第一步:首先我们先把Spring的代码准备好.如图一 (图1). 第二步:我们在桌面新建一个文件夹,如图二 (图2). 我们要在这个文件夹里新建两个夹,一个文件夹是你项目的包名,也就是我们图1的aop ...

  7. 漫谈C++:良好的编程习惯与编程要点

    以良好的方式编写C++ class 假设现在我们要实现一个复数类complex,在类的实现过程中探索良好的编程习惯. ① Header(头文件)中的防卫式声明 complex.h: # ifndef ...

  8. Linux--Tail命令

    inux tail命令用途是依照要求将指定的文件的最后部分输出到标准设备,通常是终端,通俗讲来,就是把某个档案文件的最后几行显示到终端上,假设该档案有更新,tail会自己主动刷新,确保你看到最新的档案 ...

  9. 彻底理解session

    最详细的对session的解析: https://www.tooto.cc/archives/178

  10. ROC & AUC笔记

    易懂:http://alexkong.net/2013/06/introduction-to-auc-and-roc/ 分析全面但难懂:http://mlwiki.org/index.php/ROC_ ...