Skip to content

Commit

Permalink
fix 避免递归 @autoreleasepool,出现多次release 引起的 crash 问题
Browse files Browse the repository at this point in the history
  • Loading branch information
lijianghuai committed Nov 13, 2020
1 parent 4f473c7 commit f92dc47
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
4 changes: 2 additions & 2 deletions LKDBHelper.podspec.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "LKDBHelper",
"version": "2.5.5",
"version": "2.5.6",
"summary": "全自动的插入,查询,更新,删除, an automatic database operation thread-safe and not afraid of recursive deadlock",
"description": "全面支持 NSArray,NSDictionary, ModelClass, NSNumber, NSString, NSDate, NSData, UIColor, UIImage, CGRect, CGPoint, CGSize, NSRange, int,char,float, double, long.. 等属性的自动化操作(插入和查询)",
"homepage": "https://github.com/li6185377/LKDBHelper-SQLite-ORM",
Expand All @@ -10,7 +10,7 @@
},
"source": {
"git": "https://github.com/li6185377/LKDBHelper-SQLite-ORM.git",
"tag": "2.5.5"
"tag": "2.5.6"
},
"platforms": {
"ios": "5.0",
Expand Down
41 changes: 27 additions & 14 deletions LKDBHelper/Helper/LKDBHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ @interface LKDBHelper ()
@property (nonatomic, assign) BOOL runingAutoCloseTimer;
@property (nonatomic, assign) NSInteger autoCloseDBDelayTime;

@property (nonatomic, assign) BOOL inAutoReleasePool;

@end

@implementation LKDBHelper
Expand Down Expand Up @@ -1093,14 +1095,25 @@ - (NSMutableArray *)executeOneColumnResult:(FMResultSet *)set {
return array;
}

- (void)foreachResultSet:(FMResultSet *)set block:(void(^)(void))block {
while ([set next]) {
- (void)inAutoReleaseExecuteBlock:(void(^)(void))block {
if (self.inAutoReleasePool) {
// 已在 @autoreleasepool 范围内
block();
} else {
@autoreleasepool {
self.inAutoReleasePool = YES;
block();
self.inAutoReleasePool = NO;
}
}
}

- (void)foreachResultSet:(FMResultSet *)set block:(void(^)(void))block {
while ([set next]) {
[self inAutoReleaseExecuteBlock:block];
}
}

- (NSMutableArray *)executeResult:(FMResultSet *)set Class:(Class)modelClass tableName:(NSString *)tableName {
NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
if (!modelClass) {
Expand Down Expand Up @@ -1165,19 +1178,19 @@ - (NSMutableArray *)executeResult:(FMResultSet *)set Class:(Class)modelClass tab

#pragma mark - insert operation
- (BOOL)insertToDB:(NSObject *)model {
BOOL success = NO;
@autoreleasepool {
__block BOOL success = NO;
[self inAutoReleaseExecuteBlock:^{
success = [self insertBase:model];
}
}];
return success;
}

- (void)insertToDB:(NSObject *)model callback:(void (^)(BOOL))block {
LKDBCode_Async_Begin;
BOOL success = NO;
@autoreleasepool {
__block BOOL success = NO;
[sself inAutoReleaseExecuteBlock:^{
success = [sself insertBase:model];
}
}];
if (block) {
block(success);
}
Expand Down Expand Up @@ -1290,19 +1303,19 @@ - (BOOL)insertBase:(NSObject *)model {

#pragma mark - update operation
- (BOOL)updateToDB:(NSObject *)model where:(id)where {
BOOL success = NO;
@autoreleasepool {
__block BOOL success = NO;
[self inAutoReleaseExecuteBlock:^{
success = [self updateToDBBase:model where:where];
}
}];
return success;
}

- (void)updateToDB:(NSObject *)model where:(id)where callback:(void (^)(BOOL))block {
LKDBCode_Async_Begin;
BOOL success = NO;
@autoreleasepool {
__block BOOL success = NO;
[sself inAutoReleaseExecuteBlock:^{
success = [sself updateToDBBase:model where:where];
}
}];
if (block) {
block(success);
}
Expand Down

0 comments on commit f92dc47

Please sign in to comment.