Skip to content

Commit

Permalink
优化异步操作.
Browse files Browse the repository at this point in the history
优化异步操作.
  • Loading branch information
huangzhibiao committed Jul 11, 2018
1 parent 179dda2 commit 07f1571
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 116 deletions.
2 changes: 1 addition & 1 deletion BGFMDB.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |s|
#

s.name = "BGFMDB"
s.version = "2.0.8"
s.version = "2.0.11"
s.summary = "完美支持iOS大部分类型数据的存储,同时带有 字典转模型 功能模块."

# This description is used to generate tags and improve search results.
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "BGFMDB/ViewController.m"
timestampString = "549363504.2298599"
timestampString = "552992015.768521"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "421"
Expand Down Expand Up @@ -84,77 +84,14 @@
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "BGFMDB/ViewController.m"
timestampString = "549363504.23026"
timestampString = "552992015.769009"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "202"
endingLineNumber = "202"
startingLineNumber = "199"
endingLineNumber = "199"
landmarkName = "-viewDidLoad"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "BGFMDB/libs/BG/BGDB.m"
timestampString = "549362450.501228"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "1562"
endingLineNumber = "1562"
landmarkName = "-getArray:ignoredKeys:filtModelInfoType:"
landmarkType = "7">
<Locations>
<Location
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "-[BGDB getArray:ignoredKeys:filtModelInfoType:]"
moduleName = "BGFMDB"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/biao/Documents/GitHub/BGFMDB/BGFMDB/libs/BG/BGDB.m"
timestampString = "549363504.304498"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "1562"
endingLineNumber = "1562"
offsetFromSymbolStart = "136">
</Location>
<Location
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "__47-[BGDB getArray:ignoredKeys:filtModelInfoType:]_block_invoke"
moduleName = "BGFMDB"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/biao/Documents/GitHub/BGFMDB/BGFMDB/libs/BG/BGDB.m"
timestampString = "549363504.306389"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "1562"
endingLineNumber = "1562"
offsetFromSymbolStart = "24">
</Location>
<Location
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "__destroy_helper_block_.656"
moduleName = "BGFMDB"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/biao/Documents/GitHub/BGFMDB/BGFMDB/libs/BG/BGDB.m"
timestampString = "549363504.308177"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "1562"
endingLineNumber = "1562"
offsetFromSymbolStart = "20">
</Location>
</Locations>
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
2 changes: 1 addition & 1 deletion BGFMDB/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ - (void)viewDidLoad {
/**
存储.
*/
[p bg_save];
[p bg_save];

/**
同步存储或更新.
Expand Down
18 changes: 16 additions & 2 deletions BGFMDB/libs/BG/BGDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@
@interface BGDB : NSObject
//信号量.
@property(nonatomic, strong)dispatch_semaphore_t _Nullable semaphore;
/**
调试标志
*/
@property(nonatomic,assign)BOOL debug;
/**
自定义数据库名称
*/
@property(nonatomic,copy)NSString* _Nonnull sqliteName;
//设置操作过程中不可关闭数据库(即closeDB函数无效).
/**
设置操作过程中不可关闭数据库(即closeDB函数无效).
*/
@property(nonatomic,assign)BOOL disableCloseDB;
/**
获取单例函数.
Expand All @@ -29,8 +37,14 @@
删除数据库文件.
*/
+(BOOL)deleteSqlite:(NSString*_Nonnull)sqliteName;
//事务操作
/**
事务操作.
*/
-(void)inTransaction:(BOOL (^_Nonnull)())block;
/**
添加操作到线程池
*/
-(void)addToThreadPool:(void (^_Nonnull)())block;
/**
注册数据变化监听.
@claName 注册监听的类名.
Expand Down
59 changes: 57 additions & 2 deletions BGFMDB/libs/BG/BGDB.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ @interface BGDB()
@property (nonatomic, strong) FMDatabaseQueue *queue;
@property (nonatomic, strong) FMDatabase* db;
@property (nonatomic, assign) BOOL inTransaction;
/**
多线程池
*/
@property(nonatomic,strong)NSMutableArray* _Nullable mulThreadPool;
@property(nonatomic,strong)dispatch_queue_t mulThreadPoolQueue;
/**
线程池执行状态标识
*/
@property(nonatomic,assign)BOOL threadPoolFlag;
/**
递归锁.
*/
Expand Down Expand Up @@ -70,6 +79,18 @@ -(void)destroy{
}

}
-(NSMutableArray *)mulThreadPool{
if (_mulThreadPool == nil) {
_mulThreadPool = [NSMutableArray array];
}
return _mulThreadPool;
}
-(dispatch_queue_t)mulThreadPoolQueue{
if (_mulThreadPoolQueue == nil) {
_mulThreadPoolQueue = dispatch_queue_create("BGFMDB.excuteThreadPool", DISPATCH_QUEUE_SERIAL);
}
return _mulThreadPoolQueue;
}
/**
关闭数据库.
*/
Expand Down Expand Up @@ -154,14 +175,48 @@ +(_Nonnull instancetype)shareManager{
});
return BGdb;
}

/**
添加操作到线程池
*/
-(void)addToThreadPool:(void (^_Nonnull)())block{
NSAssert(block, @"block is nil!");
NSString* key = [NSString stringWithFormat:@"b_%@",@(self.mulThreadPool.count)];
NSDictionary* dict = @{key:block};
[self.mulThreadPool addObject:dict];
[self excuteThreadPool];
}
/**
执行线程池操作
*/
-(void)excuteThreadPool{
if (!_threadPoolFlag) {
_threadPoolFlag = YES;
dispatch_async(self.mulThreadPoolQueue, ^{
do{
if(self.mulThreadPool.count>0){
NSDictionary* dict = self.mulThreadPool.firstObject;
NSString* key = [dict.allKeys firstObject];
void(^block)(void) = dict[key];
if(block){
block();
}
[self.mulThreadPool removeObjectAtIndex:0];//移除任务
}
}while(self.mulThreadPool.count>0);
_threadPoolFlag = NO;
});
}
}

//事务操作
-(void)inTransaction:(BOOL (^_Nonnull)())block{
NSAssert(block, @"block is nil!");
if([NSThread currentThread].isMainThread){//主线程直接执行
[self executeTransation:block];
}else{//子线程则延迟执行
[self.transactionBlocks addObject:block];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.2*NSEC_PER_SEC), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.2*NSEC_PER_SEC), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self executeTransationBlocks];
});
}
Expand Down Expand Up @@ -260,7 +315,7 @@ -(BOOL)removeChangeWithName:(NSString* const _Nonnull)name{
-(void)doChangeWithName:(NSString* const _Nonnull)name flag:(BOOL)flag state:(bg_changeState)state{
if(flag && self.changeBlocks.count>0){
//开一个子线程去执行block,防止死锁.
dispatch_async(dispatch_get_global_queue(0,0), ^{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0), ^{
[self.changeBlocks enumerateKeysAndObjectsUsingBlock:^(NSString* _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop){
NSString* tablename = [key componentsSeparatedByString:@"*"].firstObject;
if([name isEqualToString:tablename]){
Expand Down
Loading

0 comments on commit 07f1571

Please sign in to comment.