Skip to content

Commit

Permalink
[feat] 轻微优化部分操作下的性能
Browse files Browse the repository at this point in the history
  • Loading branch information
lijianghuai committed Jun 19, 2024
1 parent 4532c16 commit 48f2538
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
6 changes: 3 additions & 3 deletions LKDBHelper.podspec.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "LKDBHelper",
"version": "2.6.0",
"version": "2.6.1",
"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,10 +10,10 @@
},
"source": {
"git": "https://github.com/li6185377/LKDBHelper-SQLite-ORM.git",
"tag": "2.6.0"
"tag": "2.6.1"
},
"platforms": {
"ios": "5.0",
"ios": "6.0",
"osx": "10.7",
"watchos": "2.0"
},
Expand Down
30 changes: 10 additions & 20 deletions LKDBHelper/Helper/LKDBHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ - (void)runAutoCloseDBConnection {
[self closeDB];
}

/// 整个方法已经处于加锁状态
- (void)runAutoVacuumAction {
// 数据库链接已关闭
if (!self.bindingQueue) {
Expand All @@ -339,7 +340,6 @@ - (void)runAutoVacuumAction {
// 读取全局缓存文件
static NSMutableDictionary *dbAutoVaccumMap = nil;
static NSString *dbAutoVaccumPath = nil;
static dispatch_semaphore_t dbLock = NULL;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSString *cacheDirectory = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
Expand All @@ -348,29 +348,24 @@ - (void)runAutoVacuumAction {
if (!dbAutoVaccumMap) {
dbAutoVaccumMap = [NSMutableDictionary dictionary];
}
dbLock = dispatch_semaphore_create(1);
});
// 3天操作一次
NSString *dbKey = self.dbPath.lastPathComponent;
dispatch_semaphore_wait(dbLock, DISPATCH_TIME_FOREVER);
NSInteger lastTime = [[dbAutoVaccumMap objectForKey:dbKey] integerValue];
if (0 == lastTime) {
// 记录第一次运行的时间
lastTime = nowTime;
[dbAutoVaccumMap setObject:@(nowTime) forKey:dbKey];
[dbAutoVaccumMap writeToFile:dbAutoVaccumPath atomically:YES];
}
dispatch_semaphore_signal(dbLock);
if (nowTime - lastTime < 259200) { // 60 * 60 * 24 * 3
return;
}
// 执行数据压缩
[self executeSQL:@"vacuum" arguments:nil];
// 记录执行时间
dispatch_semaphore_wait(dbLock, DISPATCH_TIME_FOREVER);
[dbAutoVaccumMap setObject:@(nowTime) forKey:dbKey];
[dbAutoVaccumMap writeToFile:dbAutoVaccumPath atomically:YES];
dispatch_semaphore_signal(dbLock);
}

- (BOOL)executeSQL:(NSString *)sql arguments:(NSArray *)args {
Expand Down Expand Up @@ -594,7 +589,7 @@ @implementation LKDBHelper (DatabaseManager)
- (void)dropAllTable {
[self executeDB:^(FMDatabase *db) {
FMResultSet *set = [db executeQuery:@"select name from sqlite_master where type='table'"];
NSMutableArray *dropTables = [NSMutableArray arrayWithCapacity:0];
NSMutableArray *dropTables = [NSMutableArray array];

while ([set next]) {
NSString *tableName = [set stringForColumnIndex:0];
Expand Down Expand Up @@ -853,7 +848,7 @@ - (id)modelValueWithProperty:(LKDBProperty *)property model:(NSObject *)model {
}

- (void)asyncBlock:(void (^)(void))block {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block);
dispatch_async(dispatch_get_global_queue(0, 0), block);
}

#pragma mark - row count operation
Expand Down Expand Up @@ -1139,7 +1134,7 @@ - (void)sqlString:(NSMutableString *)sql groupBy:(NSString *)groupBy orderBy:(NS
}

- (NSMutableArray *)executeOneColumnResult:(FMResultSet *)set {
NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
NSMutableArray *array = [NSMutableArray array];

while ([set next]) {
NSString *string = [set stringForColumnIndex:0];
Expand All @@ -1159,15 +1154,10 @@ - (NSMutableArray *)executeOneColumnResult:(FMResultSet *)set {
}

- (void)inAutoReleaseExecuteBlock:(void(^)(void))block {
if (self.inAutoReleasePool) {
// 已在 @autoreleasepool 范围内
// 不在用 autoreleasepool 包一层了
// 部分情况下 容易野指针
if (block) {
block();
} else {
@autoreleasepool {
self.inAutoReleasePool = YES;
block();
self.inAutoReleasePool = NO;
}
}
}

Expand All @@ -1178,7 +1168,7 @@ - (void)foreachResultSet:(FMResultSet *)set block:(void(^)(void))block {
}

- (NSMutableArray *)executeResult:(FMResultSet *)set Class:(Class)modelClass tableName:(NSString *)tableName {
NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
NSMutableArray *array = [NSMutableArray array];
if (!modelClass) {
// 防止内存释放太慢引起的 OOM,用 autorelease 包一层
[self foreachResultSet:set block:^{
Expand Down Expand Up @@ -1636,7 +1626,7 @@ + (void)clearNoneData:(Class)modelClass columns:(NSArray *)columns {

#define LKTestDirFilename @"LKTestDirFilename111"
+ (void)clearFileWithTable:(Class)modelClass columns:(NSArray *)columns type:(NSInteger)type {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSString *testpath = nil;
switch (type) {
case 1: {
Expand Down Expand Up @@ -1672,7 +1662,7 @@ + (void)clearFileWithTable:(Class)modelClass columns:(NSArray *)columns type:(NS
NSString *querySql = [NSString stringWithFormat:@"select %@ from %@ where %@", seleteColumn, [modelClass getTableName], whereStr];
__block NSArray *dbfiles;
[[modelClass getUsingLKDBHelper] executeDB:^(FMDatabase *db) {
NSMutableArray *tempfiles = [NSMutableArray arrayWithCapacity:6];
NSMutableArray *tempfiles = [NSMutableArray array];
FMResultSet *set = [db executeQuery:querySql];

while ([set next]) {
Expand Down

0 comments on commit 48f2538

Please sign in to comment.