93 lines
3.0 KiB
Objective-C
93 lines
3.0 KiB
Objective-C
//
|
||
// DataCenter.m
|
||
// 爱鱼奇
|
||
//
|
||
// Created by imac on 15/9/21.
|
||
// Copyright © 2015年 imac. All rights reserved.
|
||
//
|
||
|
||
#import "DataCenter.h"
|
||
|
||
@implementation DataCenter
|
||
{
|
||
NSMutableDictionary *_dict;
|
||
}
|
||
//获取单例
|
||
+(instancetype)defaultDtacenter{
|
||
static DataCenter*center=nil;
|
||
|
||
@synchronized(self){
|
||
if (center==nil) {
|
||
center=[[self alloc]init];
|
||
|
||
}
|
||
}
|
||
return center;
|
||
}
|
||
//初始化
|
||
-(instancetype)init{
|
||
if (self=[super init]) {
|
||
NSString*filePath=[self getFullFilePathWithName:@"myData.archiver"];
|
||
//检测文件是否存在
|
||
//保证 _dict指向的对象 和 本地归档文件数据同步
|
||
//存在
|
||
//读档 /解归档
|
||
//成员变量变量是 对象指针 必须要拥有对象的绝对使用权 否则的话 对象可能随时释放
|
||
|
||
if ([[NSFileManager defaultManager]fileExistsAtPath:filePath]) {
|
||
_dict=[NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
|
||
|
||
}else{
|
||
//不存在
|
||
//创建 空的可变字典
|
||
//成员变量变量是 对象指针 必须要拥有对象的绝对使用权
|
||
_dict=[[NSMutableDictionary alloc]init];
|
||
|
||
}
|
||
|
||
}
|
||
return self;
|
||
}
|
||
-(void)cacheinit:(NSString*)useid
|
||
{
|
||
NSArray*arr=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
|
||
self.cache = [[EGOCache alloc] initWithCacheDirectory:[arr.firstObject stringByAppendingPathComponent:[NSString stringWithFormat:@"ifish/%@",[CommonUtils getNotNilStr:useid]]]];
|
||
[self.cache setDefaultTimeoutInterval:864000000];
|
||
}
|
||
//根据文件名 获取指定文件名的在沙盒Documents的 全路径
|
||
-(NSString*)getFullFilePathWithName:(NSString *)name{
|
||
//方法1
|
||
// NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
|
||
//方法2:获取Documents
|
||
//注:NSDocumentDirectory 路径结构 var/mobile/Containers/Data/Application/FC53241A-883C-4770-9117-694E00A41173/Documents/myData.archiver
|
||
// NSDocumentationDirectory /var/mobile/Containers/Data/Application/CFE1A228-F736-4018-B100-C8EBCD645630/Library/Documentation/myData.archiver
|
||
|
||
NSArray*arr=NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
|
||
NSString*docPath=arr[0];
|
||
//检测Documents是否存在
|
||
if ([[NSFileManager defaultManager]fileExistsAtPath:docPath]) {
|
||
|
||
}else{
|
||
// NSLog(@"Document不存在");
|
||
|
||
}
|
||
//拼接 文件在 Documents下的路径
|
||
return [docPath stringByAppendingPathComponent:name];
|
||
|
||
}
|
||
-(void)setValue:(id)value forKey:(NSString*)key{
|
||
//先存入字典
|
||
[_dict setObject:value forKey:key];
|
||
//立即归档 保证字典 和归档数据同步
|
||
BOOL ret=[NSKeyedArchiver archiveRootObject:_dict toFile:[self getFullFilePathWithName:@"myData.archiver"]];
|
||
if (!ret) {
|
||
// NSLog(@"归档失败");
|
||
}
|
||
}
|
||
//获取数据
|
||
-(id)valueForKey:(NSString*)key{
|
||
//从字典获取 _dict 和本地归档数据是同步的
|
||
return _dict[key];
|
||
}
|
||
@end
|