diff --git a/LKDBHelper.podspec.json b/LKDBHelper.podspec.json index 4948275..d9f7315 100644 --- a/LKDBHelper.podspec.json +++ b/LKDBHelper.podspec.json @@ -1,6 +1,6 @@ { "name": "LKDBHelper", - "version": "2.6.9", + "version": "2.7.0", "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", @@ -10,7 +10,7 @@ }, "source": { "git": "https://github.com/li6185377/LKDBHelper-SQLite-ORM.git", - "tag": "2.6.9" + "tag": "2.7.0" }, "platforms": { "ios": "12.0", diff --git a/LKDBHelper/Helper/LKDBHelper.m b/LKDBHelper/Helper/LKDBHelper.m index 9c92b76..14f8db9 100644 --- a/LKDBHelper/Helper/LKDBHelper.m +++ b/LKDBHelper/Helper/LKDBHelper.m @@ -66,6 +66,9 @@ @interface LKDBHelper () @property (nonatomic, assign) NSInteger autoCloseDBDelayTime; @property (nonatomic, assign) BOOL inAutoReleasePool; + +@property (atomic, assign) NSInteger latestAutoActionIndex; + @end @implementation LKDBHelper @@ -164,6 +167,7 @@ - (instancetype)initWithDBPath:(NSString *)filePath { self.lastExecuteDBTime = CFAbsoluteTimeGetCurrent(); self.autoCloseDBDelayTime = 15; self.enableAutoVacuum = YES; + self.latestAutoActionIndex = 0; [self setDBPath:filePath]; [LKDBHelper dbHelperWithPath:nil save:self]; @@ -291,8 +295,20 @@ - (void)startAutoActionsTimer { } self.runingAutoActionsTimer = YES; __weak LKDBHelper *wself = self; + + NSInteger const newAutoActionIndex = self.latestAutoActionIndex + 1; + self.latestAutoActionIndex = newAutoActionIndex; + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_global_queue(0, 0), ^{ __strong LKDBHelper *self = wself; + if (!self) { + return; + } + if (self.latestAutoActionIndex != newAutoActionIndex) { + // 当前的操作 已经不是最新那条,可以过滤该Block的执行,避免多次加锁 + return; + } + [self.threadLock lock]; [self runAutoVacuumAction]; [self runAutoCloseDBConnection];