Skip to content

Commit

Permalink
优化存取速度.
Browse files Browse the repository at this point in the history
优化存取速度.
  • Loading branch information
huangzhibiao committed Mar 24, 2017
1 parent 2266d04 commit 667cb36
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 156 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -29,53 +29,5 @@
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "BGFMDB/ViewController.m"
timestampString = "512017230.67352"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "159"
endingLineNumber = "159"
landmarkName = "-viewDidLoad"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "BGFMDB/ViewController.m"
timestampString = "512017193.181593"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "112"
endingLineNumber = "112"
landmarkName = "-viewDidLoad"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "BGFMDB/ViewController.m"
timestampString = "512017284.177599"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "163"
endingLineNumber = "163"
landmarkName = "-viewDidLoad"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
4 changes: 2 additions & 2 deletions BGFMDB/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ - (void)viewDidLoad {
/**
将People类中name等于"马云爸爸"的数据的name设为"马化腾",此接口是为了方便开发者自由扩展更深层次的查询条件逻辑.
*/
BOOL updateState = [People updateFormatSqlConditions:@"set %@=%@ where %@=%@",sqlKey(@"name"),sqlValue(@"马化腾"),sqlKey(@"name"),sqlValue(@"斯巴达")];
NSLog(@"------");
//BOOL updateState = [People updateFormatSqlConditions:@"set %@=%@ where %@=%@",sqlKey(@"name"),sqlValue(@"马化腾"),sqlKey(@"name"),sqlValue(@"斯巴达")];

/**
将People类数据中name等于"马化腾"的数据更新为当前对象的数据.
*/
Expand Down
3 changes: 3 additions & 0 deletions BGFMDB/libs/BGFMDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#import "BGTool.h"

@interface BGFMDB : NSObject
//信号量.
@property (nonatomic, strong)dispatch_semaphore_t _Nullable semaphore;
@property(nonatomic,assign)BOOL debug;
/**
获取单例函数.
Expand Down Expand Up @@ -47,6 +49,7 @@
@complete 回调的block.
*/
-(void)saveObject:(id _Nonnull)object ignoredKeys:(NSArray* const _Nullable)ignoredKeys complete:(Complete_B)complete;
-(void)saveQueueObject:(id _Nonnull)object ignoredKeys:(NSArray* const _Nullable)ignoredKeys complete:(Complete_B)complete;
/**
根据条件查询对象.
@cla 代表对应的类.
Expand Down
39 changes: 4 additions & 35 deletions BGFMDB/libs/BGFMDB.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
static const void * const BGFMDBDispatchQueueSpecificKey = &BGFMDBDispatchQueueSpecificKey;

@interface BGFMDB()
//信号量.
@property (nonatomic, strong)dispatch_semaphore_t _Nullable semaphore;
//数据库队列
@property (nonatomic, strong) FMDatabaseQueue *queue;
@property (nonatomic, strong) FMDatabase* db;
Expand Down Expand Up @@ -1047,48 +1045,19 @@ -(void)insertDictWithObject:(id)object ignoredKeys:(NSArray* const _Nullable)ign
}];

}

/**
存储一个对象.
*/
-(void)saveQueueObject:(id _Nonnull)object ignoredKeys:(NSArray* const _Nullable)ignoredKeys complete:(Complete_B)complete{
//检查是否建立了跟对象相对应的数据表
NSString* tableName = NSStringFromClass([object class]);
//获取"唯一约束"字段名
NSString* uniqueKey = [BGTool getUnique:object];
__weak typeof(self) BGSelf = self;
[self isExistWithTableName:tableName complete:^(BOOL isExist) {
__strong typeof(BGSelf) strongSelf = BGSelf;
if (!isExist){//如果不存在就新建
NSMutableArray* createKeys = [NSMutableArray arrayWithArray:[BGTool getClassIvarList:[object class] onlyKey:NO]];
//判断是否有需要忽略的key集合.
if (ignoredKeys){
for(__block int i=0;i<createKeys.count;i++){
NSString* createKey = [createKeys[i] componentsSeparatedByString:@"*"][0];
[ignoredKeys enumerateObjectsUsingBlock:^(id _Nonnull ignoreKey, NSUInteger idi, BOOL * _Nonnull stop) {
if([createKey isEqualToString:ignoreKey]){
[createKeys removeObjectAtIndex:i];
i--;
*stop = YES;
}
}];
}
}
[strongSelf createTableWithTableName:tableName keys:createKeys uniqueKey:uniqueKey complete:^(BOOL isSuccess) {
if (isSuccess){
NSString* successInfo = [NSString stringWithFormat:@"建表成功 第一次建立 %@ 对应的表",tableName];
debug(successInfo);
}
}];
}

//插入数据
[strongSelf insertDictWithObject:object ignoredKeys:ignoredKeys complete:complete];
}];
//插入数据
[self insertDictWithObject:object ignoredKeys:ignoredKeys complete:complete];

}
-(void)saveObject:(id _Nonnull)object ignoredKeys:(NSArray* const _Nullable)ignoredKeys complete:(Complete_B)complete{
dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
@autoreleasepool {
[BGTool ifNotExistWillCreateTableWithObject:object ignoredKeys:ignoredKeys];
[self saveQueueObject:object ignoredKeys:ignoredKeys complete:complete];
}
dispatch_semaphore_signal(self.semaphore);
Expand Down
4 changes: 4 additions & 0 deletions BGFMDB/libs/BGTool.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ extern NSString* _Nonnull keyPathValues(NSArray* _Nonnull keyPathValues);
根据对象获取要更新的字典.
*/
+(NSDictionary* _Nonnull)getUpdateDictWithObject:(id _Nonnull)object;
/**
如果表格不存在就新建.
*/
+(BOOL)ifNotExistWillCreateTableWithObject:(id _Nonnull)object ignoredKeys:(NSArray* const _Nullable)ignoredKeys;
/**
NSUserDefaults封装使用函数.
*/
Expand Down
34 changes: 34 additions & 0 deletions BGFMDB/libs/BGTool.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#import "BGTool.h"
#import "BGFMDB.h"

#define SqlText @"text" //数据库的字符类型
#define SqlReal @"real" //数据库的浮点类型
Expand Down Expand Up @@ -756,6 +757,39 @@ +(NSDictionary*)getUpdateDictWithObject:(id)object{
[valueDict removeObjectForKey:[NSString stringWithFormat:@"%@%@",BG,BGCreateTime]];
return valueDict;
}
/**
如果表格不存在就新建.
*/
+(BOOL)ifNotExistWillCreateTableWithObject:(id)object ignoredKeys:(NSArray* const _Nullable)ignoredKeys{
//检查是否建立了跟对象相对应的数据表
NSString* tableName = NSStringFromClass([object class]);
//获取"唯一约束"字段名
NSString* uniqueKey = [BGTool getUnique:object];
__block BOOL isExistTable;
[[BGFMDB shareManager] isExistWithTableName:tableName complete:^(BOOL isExist) {
if (!isExist){//如果不存在就新建
NSMutableArray* createKeys = [NSMutableArray arrayWithArray:[BGTool getClassIvarList:[object class] onlyKey:NO]];
//判断是否有需要忽略的key集合.
if (ignoredKeys){
for(__block int i=0;i<createKeys.count;i++){
NSString* createKey = [createKeys[i] componentsSeparatedByString:@"*"][0];
[ignoredKeys enumerateObjectsUsingBlock:^(id _Nonnull ignoreKey, NSUInteger idi, BOOL * _Nonnull stop) {
if([createKey isEqualToString:ignoreKey]){
[createKeys removeObjectAtIndex:i];
i--;
*stop = YES;
}
}];
}
}
[[BGFMDB shareManager] createTableWithTableName:tableName keys:createKeys uniqueKey:uniqueKey complete:^(BOOL isSuccess) {
isExistTable = isSuccess;
}];
}
}];

return isExistTable;
}
+(BOOL)getBoolWithKey:(NSString*)key{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
return [defaults boolForKey:key];
Expand Down
10 changes: 10 additions & 0 deletions BGFMDB/libs/NSObject+BGModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ NSMutableData,UIImage,NSDate,NSURL,NSRange,CGRect,CGSize,CGPoint,自定义对象
异步存储.
*/
-(void)saveAsync:(Complete_B)complete;
/**
同步存入对象数组.
@array 存放对象的数组.
*/
+(BOOL)saveArray:(NSArray* _Nonnull)array;
/**
异步存入对象数组.
@array 存放对象的数组.
*/
+(void)saveArrayAsync:(NSArray* _Nonnull)array complete:(Complete_B)complete;
/**
同步存储或更新.
当自定义“唯一约束”时可以使用此接口存储更方便,当"唯一约束"的数据存在时,此接口会更新旧数据,没有则存储新数据.
Expand Down
Loading

0 comments on commit 667cb36

Please sign in to comment.