沙盒簡述:
每一個(gè)APP都有一個(gè)存儲(chǔ)空間一疯,就是沙盒计露。
APP之間不能相互通信尊惰。
沙盒根目錄結(jié)構(gòu):Documents郁轻、Library、temp新啼。
- 1.在iOS開發(fā)中追城,經(jīng)常會(huì)用到數(shù)據(jù)緩存的功能,我的這個(gè)Demo是基于沙盒路徑機(jī)制做的緩存;如果程序并沒有被關(guān)閉燥撞,一直在運(yùn)行,那么此時(shí)內(nèi)存緩存中有數(shù)據(jù)座柱,硬盤緩存中有數(shù)據(jù)。如果此時(shí)再次請(qǐng)求數(shù)據(jù)物舒,直接使用內(nèi)存緩存中的數(shù)據(jù)即可色洞。將服務(wù)器第一次返回的數(shù)據(jù)保存在沙盒里面。這樣在手機(jī)斷網(wǎng)的情況下可以從本地讀取數(shù)據(jù)了冠胯。
- 2.緩存的注意事項(xiàng)
緩存的設(shè)置需要根據(jù)具體的情況考慮火诸,如果請(qǐng)求某個(gè)URL的返回?cái)?shù)據(jù):
(1)經(jīng)常更新:不能用緩存荠察!比如股票置蜀、彩票數(shù)據(jù)
(2)一成不變:果斷用緩存
∠づ琛(3)偶爾更新:可以定期更改緩存策略 或者 清除緩存
提示:如果大量使用緩存盯荤,會(huì)越積越大,建議定期清除緩存(接下來完善焕盟,添加) - 3.再次介紹一下沙盒
文件都在個(gè)人用戶名文件夾下的一個(gè)隱藏文件夾里秋秤,中文叫資源庫,他的目錄其實(shí)是Library脚翘。因?yàn)閼?yīng)用是在沙箱(sandbox)中的灼卢,在文件讀寫權(quán)限上受到限制,只能在幾個(gè)目錄下讀寫文件: - Documents:用于存儲(chǔ)用戶數(shù)據(jù)来农,iTunes備份和恢復(fù)的時(shí)候會(huì)包括此目錄鞋真,所以,蘋果建議將程序中建立的或在程序中瀏覽到的文件數(shù)據(jù)保存在該目錄下沃于。
- tmp:存放臨時(shí)文件灿巧,這個(gè)可以放一些當(dāng)APP退出后不再需要的文件,iTunes不會(huì)備份和恢復(fù)此目錄揽涮,此目錄下文件可能會(huì)在應(yīng)用退出后刪除
- Library/Caches:存放緩存文件,iTunes不會(huì)備份此目錄饿肺,此目錄下文件不會(huì)在應(yīng)用退出刪除
ZJDataCache.h文件代碼
#import <Foundation/Foundation.h>
/*
緩存:臨時(shí)保存數(shù)據(jù)的一種形式
通過接口請(qǐng)求數(shù)據(jù),將數(shù)據(jù)在本地保存一份(將數(shù)據(jù)保存到本地文件).當(dāng)在一段時(shí)間內(nèi)重新訪問當(dāng)前界面時(shí),不必從接口請(qǐng)求數(shù)據(jù),而是將本地的數(shù)據(jù)拿來使用.這個(gè)過程叫做對(duì)數(shù)據(jù)的緩存.
作用:為用戶節(jié)省大量的流量,提高效率,提高用戶體驗(yàn).
缺點(diǎn):不能拿到實(shí)時(shí)的數(shù)據(jù)
*/
@interface ZJDataCache : NSObject
//創(chuàng)建單例對(duì)象
+(ZJDataCache *)sharedCache;
//存數(shù)據(jù)
-(BOOL)saveDataWithData:(NSData *)data andStringName:(NSString *)name;
//取數(shù)據(jù)
-(NSData *)getDataWithStringName:(NSString *)name;
@end
ZJDataCache.m文件
#import "ZJDataCache.h"
#import "NSString+Hashing.h"
@interface ZJDataCache ()
@property (nonatomic,assign) NSTimeInterval invaliteTime;//有效時(shí)間
@end
@implementation ZJDataCache
//創(chuàng)建單例對(duì)象
static ZJDataCache cache = nil;
/*
* @author zhengju, 16-06-30 17:06:27
*
* @brief 單例創(chuàng)建緩存對(duì)象
*
* @return 單例對(duì)象
/
+(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 初始化的時(shí)候返設(shè)置過期時(shí)間
*
* @return 對(duì)象
/
-(id)init{
if (self = [super init]) {
_invaliteTime = 6060;//以秒為單位
}
return self;
}
/*
* @author zhengju, 16-06-30 17:06:43
*
* @brief 存數(shù)據(jù)
*
* @param data 緩存Data數(shù)據(jù)
* @param name 段路徑蒋困,一般用請(qǐng)求數(shù)據(jù)的短URL來傳值
*
* @return 是否保存數(shù)據(jù)成功
*/
-(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(@"創(chuàng)建失敗");
return NO;
}
//先將文件名字進(jìn)行加密處理
//MD5:一種加密方式,通過MD5加密會(huì)得到一個(gè)16進(jìn)制的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 根據(jù)路徑查找Data數(shù)據(jù)
*
* @param name 段路徑
*
* @return 返回緩存的在段路徑下的Data數(shù)據(jù)
*/
-(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;
}
//判斷數(shù)據(jù)是否過期
NSTimeInterval invalitTime = [[NSDate date] timeIntervalSinceDate:[self getLastModefityDateWithFile:path]];
if (invalitTime >= _invaliteTime) {
return nil;
}
//取數(shù)據(jù)
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沙盒目錄結(jié)構(gòu)解析
歡迎轉(zhuǎn)載,轉(zhuǎn)載請(qǐng)注明出處敬辣。
github下載地址:https://github.com/zhengju/DataCache