沙盒简述:
每一个APP都有一个存储空间,就是沙盒。
APP之间不能相互通信。
沙盒根目录结构:Documents、Library、temp。
- 1.在iOS开发中,经常会用到数据缓存的功能,我的这个Demo是基于沙盒路径机制做的缓存;如果程序并没有被关闭,一直在运行,那么此时内存缓存中有数据,硬盘缓存中有数据。如果此时再次请求数据,直接使用内存缓存中的数据即可。将服务器第一次返回的数据保存在沙盒里面。这样在手机断网的情况下可以从本地读取数据了。
- 2.缓存的注意事项
缓存的设置需要根据具体的情况考虑,如果请求某个URL的返回数据:
(1)经常更新:不能用缓存!比如股票、彩票数据
(2)一成不变:果断用缓存
(3)偶尔更新:可以定期更改缓存策略 或者 清除缓存
提示:如果大量使用缓存,会越积越大,建议定期清除缓存(接下来完善,添加) - 3.再次介绍一下沙盒
文件都在个人用户名文件夹下的一个隐藏文件夹里,中文叫资源库,他的目录其实是Library。因为应用是在沙箱(sandbox)中的,在文件读写权限上受到限制,只能在几个目录下读写文件: - Documents:用于存储用户数据,iTunes备份和恢复的时候会包括此目录,所以,苹果建议将程序中建立的或在程序中浏览到的文件数据保存在该目录下。
- tmp:存放临时文件,这个可以放一些当APP退出后不再需要的文件,iTunes不会备份和恢复此目录,此目录下文件可能会在应用退出后删除
- Library/Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除
ZJDataCache.h文件代码
#import <Foundation/Foundation.h>
/*
缓存:临时保存数据的一种形式
通过接口请求数据,将数据在本地保存一份(将数据保存到本地文件).当在一段时间内重新访问当前界面时,不必从接口请求数据,而是将本地的数据拿来使用.这个过程叫做对数据的缓存.
作用:为用户节省大量的流量,提高效率,提高用户体验.
缺点:不能拿到实时的数据
*/
@interface ZJDataCache : NSObject
//创建单例对象
+(ZJDataCache *)sharedCache;
//存数据
-(BOOL)saveDataWithData:(NSData *)data andStringName:(NSString *)name;
//取数据
-(NSData *)getDataWithStringName:(NSString *)name;
@end
ZJDataCache.m文件
#import "ZJDataCache.h"
#import "NSString+Hashing.h"
@interface ZJDataCache ()
@property (nonatomic,assign) NSTimeInterval invaliteTime;//有效时间
@end
@implementation ZJDataCache
//创建单例对象
static ZJDataCache cache = nil;
/*
* @author zhengju, 16-06-30 17:06:27
*
* @brief 单例创建缓存对象
*
* @return 单例对象
/
+(ZJDataCache )sharedCache{
@synchronized(self){
if (!cache) {
cache = [[[ZJDataCache class] alloc]init];
}
}
return cache;
}
+(instancetype)allocWithZone:(struct _NSZone )zone{
@synchronized(self){
if (!cache) {
cache = [super allocWithZone:zone];
}
}
return cache;
}
/
* @author zhengju, 16-06-30 17:06:49
*
* @brief 初始化的时候返设置过期时间
*
* @return 对象
/
-(id)init{
if (self = [super init]) {
_invaliteTime = 6060;//以秒为单位
}
return self;
}
/*
* @author zhengju, 16-06-30 17:06:43
*
* @brief 存数据
*
* @param data 缓存Data数据
* @param name 段路径,一般用请求数据的短URL来传值
*
* @return 是否保存数据成功
*/
-(BOOL)saveDataWithData:(NSData *)data andStringName:(NSString *)name{
//获取路径
NSString *path = [NSString stringWithFormat:@"%@/Documents/Cache/",NSHomeDirectory()];//沙盒路径
NSFileManager manager = [NSFileManager defaultManager];
BOOL isSuc = [manager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
if (!isSuc) {
NSLog(@"创建失败");
return NO;
}
//先将文件名字进行加密处理
//MD5:一种加密方式,通过MD5加密会得到一个16进制的32位的文件(固定长度)
name = [name MD5Hash];
//获取的完整路径
NSString allPath = [NSString stringWithFormat:@"%@%@",path,name];
BOOL isWriteSuc = [data writeToFile:allPath atomically:YES];//写文件
return isWriteSuc;
}
/
* @author zhengju, 16-06-30 17:06:44
*
* @brief 根据路径查找Data数据
*
* @param name 段路径
*
* @return 返回缓存的在段路径下的Data数据
*/
-(NSData *)getDataWithStringName:(NSString *)name{
NSString *tempName = [name MD5Hash];
NSString *path = [NSString stringWithFormat:@"%@/Documents/Cache/%@",NSHomeDirectory(),tempName];
// NSLog(@"--path----->>%@",path);
//判断文件是否存在
NSFileManager *manage = [NSFileManager defaultManager];
if (![manage fileExistsAtPath:path]) {
NSLog(@"文件不存在");
return nil;
}
//判断数据是否过期
NSTimeInterval invalitTime = [[NSDate date] timeIntervalSinceDate:[self getLastModefityDateWithFile:path]];
if (invalitTime >= _invaliteTime) {
return nil;
}
//取数据
NSData *data = [NSData dataWithContentsOfFile:path];
return data;
}
//获取最后修改文件的日期
-(NSDate *)getLastModefityDateWithFile:(NSString *)path{
NSFileManager *manager = [NSFileManager defaultManager];
NSDictionary dic = [manager attributesOfItemAtPath:path error:nil];
/
NSFileCreationDate = "2015-08-10 03:38:15 +0000";
NSFileExtensionHidden = 0;
NSFileGroupOwnerAccountID = 20;
NSFileGroupOwnerAccountName = staff;
NSFileModificationDate = "2015-08-10 03:38:15 +0000";
NSFileOwnerAccountID = 501;
NSFilePosixPermissions = 420;
NSFileReferenceCount = 1;
NSFileSize = 17090;
NSFileSystemFileNumber = 9204533;
NSFileSystemNumber = 16777217;
NSFileType = NSFileTypeRegular;
*/
return dic[NSFileModificationDate];
}
@end
本文参考: iOS沙盒目录结构解析
欢迎转载,转载请注明出处。
github下载地址:https://github.com/zhengju/DataCache