- UserDefaults
- Plist
存储NSArrayNSDictionary - 归档
针对单一的对象。 - FMDB
FMDB是iOS平台的SQLite数据库框架
FMDB以OC的方式封装了SQLite的C语言API
使用起来更加面向对象,省去了很多麻烦、冗余的C语言代码
提供了多线程安全的数据库操作方法,有效地防止数据混乱 - LevelDB 链接
当你的app需要处理的数据上万条时,FMDB显得很吃力,这个吃力表现在效率上,数据库的操作,肯定都有加锁,上万条数据排队去执行,加上反序列化的一些操作, - CoreData 链接
CoreData出现在iOS 3中,是苹果推出的一个数据存储框架。CoreData提供了一种对象关系映射(ORM)的存储关系,类似于Java的hibernate框架。CoreData可以将OC对象存储到数据库中,也可以将数据库中的数据转化为OC对象,在这个过程中不需要手动编写任何SQL语句。
简单地将对象保存到沙河路径的思路
项目中我们可能需要保存一些数组,同时数组中保存的是一些对象类型数据,如果使我们创建的对象,需要让对象遵守 NSCoding 协议,采用归档来存储数据。
初始化一个单例,创建沙盒路径地址
-(instancetype)init{
if (self = [super init]) {
self.documentDefaultPath = [self getLocalDataDefaultPathWithOption:JHLocalDataDocuments];
self.cacheDefaultPath = [self getLocalDataDefaultPathWithOption:JHLocalDataCache];
self.tempDefaultPath = [self getLocalDataDefaultPathWithOption:JHLocalDataTemp];
self.libraryDefaultPath = [self getLocalDataDefaultPathWithOption:JHLocalDataLibrary];
self.perferenceDefaultPath = [self getLocalDataDefaultPathWithOption:JHLocalDataPerferences];
}
return self;
}
/** data */
-(void)saveData:(NSData *)data withDataType:(JHLocalDataType)type WithKey:(NSString *)key withisSucess:(void (^)(BOOL))complementBack{
if (data.length == 0 || !type || !key) {
return;
}
NSString *path = [self getDefaultPathWithDataType:type withKey:key];
BOOL isSucess;
isSucess = [data writeToFile:path atomically:YES];
complementBack(isSucess);
}
- (NSData *)getDataWithDataType:(JHLocalDataType)type andKey:(NSString *)key{
if (!type || !key) {
return nil;
}
NSString *path = [self getDefaultPathWithDataType:type withKey:key];
NSData *data = [NSData dataWithContentsOfFile:path];
return data;
}
/** string */
-(void)saveString:(NSString *)string withDataType:(JHLocalDataType)type WithKey:(NSString *)key withisSucess:(void (^)(BOOL))complementBack{
if (string.length == 0 || !type || !key) {
return;
}
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
[self saveData:data withDataType:type WithKey:key withisSucess:^(BOOL isSucess) {
complementBack(isSucess);
}];
}
-(NSString *)getStringWithDataType:(JHLocalDataType)type andKey:(NSString *)key{
if (!type || !key) {
return nil;
}
NSData *data = [self getDataWithDataType:type andKey:key];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return string;
}
/** array */
-(void)saveArray:(NSArray *)array withDataType:(JHLocalDataType)type WithKey:(NSString *)key withisSucess:(void (^)(BOOL))complementBack{
if (!array || !type || !key) {
return;
}
NSString *path = [self getDefaultPathWithDataType:type withKey:key];
BOOL isSucess;
isSucess = [array writeToFile:path atomically:YES];
complementBack(isSucess);
}
- (NSArray *)getArrayWithDataType:(JHLocalDataType)type andKey:(NSString *)key{
if (!type || !key) {
return nil;
}
NSString *path = [self getDefaultPathWithDataType:type withKey:key];
NSArray *array = [[NSArray alloc] initWithContentsOfFile:path];
return array;
}
/** dictionary */
- (void)saveDictionary:(NSDictionary *)dictionry withDataType:(JHLocalDataType)type WithKey:(NSString *)key withisSucess:(void (^)(BOOL))complementBack{
if (!dictionry || !type || !key) {
return;
}
NSString *path = [self getDefaultPathWithDataType:type withKey:key];
BOOL isSucess;
isSucess = [dictionry writeToFile:path atomically:YES];
complementBack(isSucess);
}
-(NSDictionary *)getDictionaryWithDataType:(JHLocalDataType)type andKey:(NSString *)key{
if (!type || !key) {
return nil;
}
NSString *path = [self getDefaultPathWithDataType:type withKey:key];
NSDictionary *dic = [[NSDictionary alloc] initWithContentsOfFile:path];
return dic;
}
/** image */
-(void)saveImage:(UIImage *)image withDataType:(JHLocalDataType)type WithKey:(NSString *)key withisSucess:(void (^)(BOOL))complementBack{
if (!image || !type || !key) {
return;
}
NSData *data = UIImagePNGRepresentation(image);
[self saveData:data withDataType:JHLocalDataDocuments WithKey:key withisSucess:^(BOOL isSucess) {
complementBack(isSucess);
}];
}
-(UIImage *)getImageWithDataType:(JHLocalDataType)type andKey:(NSString *)key{
if (!type || !key) {
return nil;
}
NSData *data = [self getDataWithDataType:type andKey:key];
UIImage *image = [UIImage imageWithData:data];
return image;
}
/** object */
-(void)saveObject:(id)object withDataType:(JHLocalDataType)type WithKey:(NSString *)key withisSucess:(void (^)(BOOL))complementBack{
if (!object || !type || !key) {
return;
}
NSString *path = [self getDefaultPathWithDataType:type withKey:key];
BOOL isSucess;
isSucess = [NSKeyedArchiver archiveRootObject:object toFile:path];
complementBack(isSucess);
}
- (id)getObjectWithDataType:(JHLocalDataType)type andKey:(NSString *)key{
if (!type || !type) {
return nil;
}
NSString *path = [self getDefaultPathWithDataType:type withKey:key];
NSData *data = [NSData dataWithContentsOfFile:path];
id object = [NSKeyedUnarchiver unarchiveObjectWithData:data];
return object;
}
路径管理
#pragma mark - pathManager
- (NSString *)getDefaultPathWithDataType:(JHLocalDataType)type withKey:(NSString *)key{
NSString *path;
switch (type) {
case JHLocalDataDocuments:
{
path = self.documentDefaultPath;
}
break;
case JHLocalDataCache:
{
path = self.cacheDefaultPath;
}
break;
case JHLocalDataTemp:
{
path = self.tempDefaultPath;
}
break;
case JHLocalDataLibrary:
{
path = self.libraryDefaultPath;
}
break;
case JHLocalDataPerferences:
{
path = self.perferenceDefaultPath;
}
break;
default:
break;
}
[self creatDataPathWith:type];
return path ? [path stringByAppendingPathComponent:key] : @"";
}
/** makeAndSaveDefaultPath */
- (NSString *)getLocalDataDefaultPathWithOption:(JHLocalDataType)option{
NSString *path;
switch (option) {
case JHLocalDataDocuments:
{
path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
}
break;
case JHLocalDataCache:
{
path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
}
break;
case JHLocalDataTemp:
{
path = NSTemporaryDirectory();
}
break;
case JHLocalDataLibrary:
{
path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).firstObject;
}
break;
case JHLocalDataPerferences:
{
path = NSSearchPathForDirectoriesInDomains(NSPreferencePanesDirectory, NSUserDomainMask, YES).firstObject;
}
break;
default:
break;
}
path ? [path stringByAppendingString:JHLocalDefaultPath] : @"";
return path;
}
/** makeDocument */
- (void)creatDataPathWith:(JHLocalDataType)type{
switch (type) {
case JHLocalDataDocuments:
{
BOOL isExist;
[self.fileManager fileExistsAtPath:self.documentDefaultPath isDirectory:&isExist];
if (!isExist) {
[self.fileManager createDirectoryAtPath:self.documentDefaultPath withIntermediateDirectories:YES attributes:nil error:nil];
}
}
break;
case JHLocalDataPerferences:
{
BOOL isExist;
[self.fileManager fileExistsAtPath:self.perferenceDefaultPath isDirectory:&isExist];
if (!isExist) {
[self.fileManager createDirectoryAtPath:self.perferenceDefaultPath withIntermediateDirectories:YES attributes:nil error:nil];
}
}
break;
case JHLocalDataLibrary:
{
BOOL isExist;
[self.fileManager fileExistsAtPath:self.libraryDefaultPath isDirectory:&isExist];
if (!isExist) {
[self.fileManager createDirectoryAtPath:self.libraryDefaultPath withIntermediateDirectories:YES attributes:nil error:nil];
}
}
break;
case JHLocalDataCache:
{
BOOL isExist;
[self.fileManager fileExistsAtPath:self.cacheDefaultPath isDirectory:&isExist];
if (!isExist) {
[self.fileManager createDirectoryAtPath:self.cacheDefaultPath withIntermediateDirectories:YES attributes:nil error:nil];
}
}
break;
case JHLocalDataTemp:
{
BOOL isExist;
[self.fileManager fileExistsAtPath:self.tempDefaultPath isDirectory:&isExist];
if (!isExist) {
[self.fileManager createDirectoryAtPath:self.tempDefaultPath withIntermediateDirectories:YES attributes:nil error:nil];
}
}
break;
default:
break;
}
}
Demo下载
参考
http://www.code4app.com/forum.php?mod=viewthread&tid=11657&extra=page%3D1%26filter%3Dsortid%26sortid%3D1
http://www.cocoachina.com/ios/20160729/17245.html