本人之前寫過一個數據模型存儲方法,是我在上上家公司作緩存框架的時候寫的,具體也就是在FMDB的基礎上封裝,直接對數據模型進行存儲操做,以下:git
《iOS-基於FMDB的操做封裝,模型對象的增刪改查》github
最近本人也不是很忙,加之在Python項目中用到Redis和在APP中用到NSUserDefaults,我就想作一個便捷的基於數據庫的鍵值對存儲方案,固然這個方案並不比Redis優秀,咱們也知道Redis是直接從內存中讀取,因此速度方面無法說,可是在APP中咱們不能把鍵值對放在內存中吧,原本APP的內存就不寬裕!sql
ZFJRedisLib主要包含ZFJRedis和ZFJTable兩部分,ZFJRedis主要是用於鍵值對存儲,使用簡單方便;ZFJTable主要是對於數據模型進行增刪改查操做!所有API以下:
數據庫
ZFJRedis將會自動在本地建立一個ZFJRedis的表,用於存儲鍵值對數據;數組
字符串-NSString緩存
// NSString [ZFJRedis zfj_setValue:@"zhangfujie1128222====" forKey:@"name"]; // 取值 NSString *value = [ZFJRedis zfj_valueForKey:@"name"]; NSLog(@"%@",value);
數字對象-NSNumber框架
// NSNumber [ZFJRedis zfj_setValue:@(20) forKey:@"age"]; // 取值 NSNumber *age = [ZFJRedis zfj_valueForKey:@"age"]; NSLog(@"%@",age);
數組-NSArray學習
// NSArray [ZFJRedis zfj_setValue:@[@"111", @(222)] forKey:@"array1"]; // 取值 NSArray *array = [ZFJRedis zfj_valueForKey:@"array1"]; NSLog(@"%@",array);
字典-NSDictionaryspa
// NSDictionary NSDictionary *dict = @{@"name":@"zhangfujie", @"age":@(22), @"sex":@"man"}; [ZFJRedis zfj_setValue:dict forKey:@"zfj_infors"]; // 取值 NSDictionary *dict_value = [ZFJRedis zfj_valueForKey:@"zfj_infors"]; NSLog(@"%@",dict_value);
數據-NSData.net
// NSData NSData *data = [NSJSONSerialization dataWithJSONObject:@[@"aaaaa", @"bbbbb"] options:NSJSONWritingPrettyPrinted error:nil]; [ZFJRedis zfj_setValue:data forKey:@"arrayData"]; // 取值 NSData *data_new = [ZFJRedis zfj_valueForKey:@"arrayData"]; NSLog(@"data_new == %@",data_new);
數據模型-model
// NSObject 數據模型 ZFJModel *model = [[ZFJModel alloc] init]; model.name = @"張福傑"; model.age = 22; // 存 [ZFJRedis zfj_setValue:model forKey:@"model"]; // 取值 ZFJModel *model_my = [ZFJRedis zfj_valueForKey:@"model"]; NSLog(@"%@",model_my.name); NSLog(@"%ld",model_my.age);
批量存數據
//根據字典賦值 NSMutableDictionary *value_dict = [[NSMutableDictionary alloc] init]; [value_dict setValue:@"aaaaaaaa" forKey:@"key_string"]; [value_dict setValue:@(111) forKey:@"key_number"]; [value_dict setValue:@[@"wwwwwww11111", @(222)] forKey:@"key_array"]; [value_dict setValue:@{@"name":@"zhangfujie", @"age":@(22)} forKey:@"key_dict"]; [value_dict setValue:model_my forKey:@"key_model"]; [ZFJRedis zfj_setValuesForKeysWithDictionary:value_dict];
批量取數據
NSDictionary *dict = [ZFJRedis zfj_dictionaryWithValuesForKeys:value_dict.allKeys]; NSLog(@"dict == %@",dict);
獲取所有keys
// 獲取所有keys NSArray *keys = [ZFJRedis zfj_allKeys]; NSLog(@"keys == %@",keys);
獲取所有values
// 獲取所有values NSArray *values = [ZFJRedis zfj_allValues]; NSLog(@"values == %@",values);
獲取所有條數
// 獲取所有條數 NSInteger count = [ZFJRedis zfj_count]; NSLog(@"count == %ld",count);
判斷key是否存在
// 判斷當前key是否存在 BOOL isExist = [ZFJRedis zfj_isExistkey:@"name"]; if(isExist){ NSLog(@"name 存在"); }else{ NSLog(@"name 不存在"); }
根據key刪除
// 刪除 [ZFJRedis zfj_removeObjectForKey:@"array"];
根據keys刪除
// 根據keys刪除數據 NSArray *key_list = @[@"name", @"age"]; [ZFJRedis zfj_removeObjectsForKeys:key_list];
刪除所有
// 刪除所有 [ZFJRedis zfj_removeAllObjects];
ZFJTable是根據數據模型自動建立表,直接根據模型進行增刪改查操做的!
// 建立表 BOOL isScu = [ZFJTable zfj_createTable:[ZFJModel class]]; if(isScu){ NSLog(@"ZFJModel表建立成功"); }else{ NSLog(@"ZFJModel表建立失敗"); }
//刪除表 BOOL isScu = [ZFJTable zfj_removeTable:[ZFJModel class]]; if(isScu){ NSLog(@"ZFJModel表刪除成功"); }else{ NSLog(@"ZFJModel表刪除失敗"); }
// 刪除數據庫 BOOL isScu = [ZFJTable zfj_removeAllTables]; if(isScu){ NSLog(@"數據庫刪除成功"); }else{ NSLog(@"數據庫刪除失敗"); }
// 插入一條數據 ZFJModel *model_0 = [[ZFJModel alloc] init]; model_0.name = @"張福傑"; model_0.age = 22; model_0.gender = @"男"; [ZFJTable zfj_insertModel:model_0 completed:^(NSError * _Nonnull error) { NSLog(@"error:%@",error); }]; // 插入多條數據 ZFJModel *model_0 = [[ZFJModel alloc] init]; model_0.name = @"張福傑"; model_0.age = 22; model_0.gender = @"男"; ZFJModel *model_1 = [[ZFJModel alloc] init]; model_1.name = @"大白菜"; model_1.age = 88; model_1.gender = @"女"; NSArray *models = @[model_0, model_1]; [ZFJTable zfj_insertModels:models completed:^(NSError * _Nonnull error) { NSLog(@"error:%@",error); }];
// 刪除數據 BOOL isScu = [ZFJTable zfj_deleteModel:[ZFJModel class] where:@"name = '張福傑'"]; if(isScu){ NSLog(@"數據刪除成功"); }else{ NSLog(@"數據刪除失敗"); }
// 修改數據 ZFJModel *model_2 = [[ZFJModel alloc] init]; model_2.name = @"大白菜"; model_2.age = 22; model_2.gender = @"女"; [ZFJTable zfj_updateModel:model_2 byKey:@"name" completed:^(NSError * _Nonnull error) { NSLog(@"error:%@",error); }]; // 修改多條數據 ZFJModel *model_3 = [[ZFJModel alloc] init]; model_3.name = @"張福傑"; model_3.age = 222; model_3.gender = @"男男"; ZFJModel *model_4 = [[ZFJModel alloc] init]; model_4.name = @"大白菜"; model_4.age = 222; model_4.gender = @"女女"; NSArray *models = @[model_3, model_4]; [ZFJTable zfj_updateModels:models byKey:@"name" completed:^(NSError * _Nonnull error) { NSLog(@"error:%@",error); }];
// 查詢所有數據 [ZFJTable zfj_selectTable:[ZFJModel class] completed:^(NSError * _Nonnull error, NSArray * _Nonnull models) { for (ZFJModel *model in models) { NSLog(@"%@",model.name); } }]; // 條件查詢 [ZFJTable zfj_selectTable:[ZFJModel class] where:@"name = '張福傑'" completed:^(NSError * _Nonnull error, NSArray * _Nonnull models) { for (ZFJModel *model in models) { NSLog(@"%@",model.name); } }]; // 查詢數據條數 NSInteger count = [ZFJTable zfj_selectTableCount:[ZFJModel class]]; NSLog(@"count == %ld",count); // 根據條件查詢數量 NSDictionary *parameters = @{@"name":@"張福傑", @"age":@(22)}; NSString *sqlStr = @"name = '張福傑' and age = '22'"; NSInteger count = [ZFJTable zfj_selectTableCount:[ZFJModel class] where:sqlStr]; NSLog(@"count == %ld",count);
// 向某個表添加一個字段 BOOL isScu = [ZFJTable zfj_addProperty:[ZFJModel class] propertyName:@"newPro_1"]; if(isScu){ NSLog(@"字段添加成功"); }else{ NSLog(@"字段添加失敗"); }
// 最後一條錯誤 NSError *error = [ZFJTable zfj_lastTableError]; NSLog(@"error == %@",error);
歡迎各位大佬提供寶貴的建議和意見,也歡迎你們進羣或加本人QQ一塊兒交流學習!