- UserDefaults
- Plist
存儲NSArrayNSDictionary - 歸檔
針對單一的對象。 - FMDB
FMDB是iOS平臺的SQLite數(shù)據(jù)庫框架
FMDB以OC的方式封裝了SQLite的C語言API
使用起來更加面向?qū)ο螅∪チ撕芏嗦闊⑷哂嗟腃語言代碼
提供了多線程安全的數(shù)據(jù)庫操作方法倘待,有效地防止數(shù)據(jù)混亂 - LevelDB 鏈接
當你的app需要處理的數(shù)據(jù)上萬條時挠阁,F(xiàn)MDB顯得很吃力,這個吃力表現(xiàn)在效率上孝常,數(shù)據(jù)庫的操作苟跪,肯定都有加鎖廷痘,上萬條數(shù)據(jù)排隊去執(zhí)行,加上反序列化的一些操作件已, - CoreData 鏈接
CoreData出現(xiàn)在iOS 3中笋额,是蘋果推出的一個數(shù)據(jù)存儲框架。CoreData提供了一種對象關系映射(ORM)的存儲關系篷扩,類似于Java的hibernate框架兄猩。CoreData可以將OC對象存儲到數(shù)據(jù)庫中,也可以將數(shù)據(jù)庫中的數(shù)據(jù)轉(zhuǎn)化為OC對象,在這個過程中不需要手動編寫任何SQL語句枢冤。
簡單地將對象保存到沙河路徑的思路
項目中我們可能需要保存一些數(shù)組鸠姨,同時數(shù)組中保存的是一些對象類型數(shù)據(jù),如果使我們創(chuàng)建的對象淹真,需要讓對象遵守 NSCoding 協(xié)議讶迁,采用歸檔來存儲數(shù)據(jù)。
初始化一個單例核蘸,創(chuàng)建沙盒路徑地址
-(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;
}
}
參考
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