From 03bddd2ef7bc631f5b06e5e708a2b153b165f8e6 Mon Sep 17 00:00:00 2001 From: lijianghuai Date: Fri, 13 Dec 2024 15:06:56 +0800 Subject: [PATCH] =?UTF-8?q?[pref]=20=E9=81=BF=E5=85=8D=E6=AF=8F=E6=AC=A1?= =?UTF-8?q?=20Auto=20Actions=20Block=20=E9=83=BD=E6=89=A7=E8=A1=8C=20?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E5=8A=A0=E9=94=81=20=EF=BC=8C=E5=BD=B1?= =?UTF-8?q?=E5=93=8D=E6=95=B4=E4=BD=93=20DB=20=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LKDBHelper.podspec.json | 4 ++-- LKDBHelper/Helper/LKDBHelper.m | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) 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];