diff --git a/LKDBHelper.podspec.json b/LKDBHelper.podspec.json index f0decc8..afcccd6 100644 --- a/LKDBHelper.podspec.json +++ b/LKDBHelper.podspec.json @@ -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", @@ -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" }, diff --git a/LKDBHelper/Helper/LKDBHelper.m b/LKDBHelper/Helper/LKDBHelper.m index 958685a..bc6d8dc 100644 --- a/LKDBHelper/Helper/LKDBHelper.m +++ b/LKDBHelper/Helper/LKDBHelper.m @@ -322,6 +322,7 @@ - (void)runAutoCloseDBConnection { [self closeDB]; } +/// 整个方法已经处于加锁状态 - (void)runAutoVacuumAction { // 数据库链接已关闭 if (!self.bindingQueue) { @@ -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; @@ -348,11 +348,9 @@ - (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) { // 记录第一次运行的时间 @@ -360,17 +358,14 @@ - (void)runAutoVacuumAction { [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 { @@ -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]; @@ -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 @@ -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]; @@ -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; - } } } @@ -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:^{ @@ -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: { @@ -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]) {