Skip to content

Commit 7fe646f

Browse files
添加关闭数据库连接的方法,优化 openFlags
1 parent 4c3bfe1 commit 7fe646f

File tree

2 files changed

+59
-43
lines changed

2 files changed

+59
-43
lines changed

LKDBHelper/Helper/LKDBHelper.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ NS_ASSUME_NONNULL_BEGIN
2929

3030
/**
3131
* @brief filepath the use of : "documents/db/" + fileName + ".db"
32+
* add to global cache with instance created
3233
* refer: FMDatabase.h + (instancetype)databaseWithPath:(NSString *)inPath;
3334
*/
3435
- (instancetype)initWithDBName:(NSString *)dbname;
@@ -41,6 +42,11 @@ NS_ASSUME_NONNULL_BEGIN
4142
- (instancetype)initWithDBPath:(NSString *)filePath;
4243
- (void)setDBPath:(NSString *)filePath;
4344

45+
/**
46+
* @brief closing a database connection and remove instance for global cache
47+
*/
48+
- (void)closeDB;
49+
4450
/**
4551
* @brief current encryption key.
4652
*/

LKDBHelper/Helper/LKDBHelper.m

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
#import "LKDBHelper.h"
1010
#import <sqlite3.h>
1111

12+
#ifndef SQLITE_OPEN_FILEPROTECTION_NONE
13+
#define SQLITE_OPEN_FILEPROTECTION_NONE 0x00400000
14+
#endif
15+
16+
#define LKDBOpenFlags (SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX | SQLITE_OPEN_SHAREDCACHE | SQLITE_OPEN_FILEPROTECTION_NONE)
17+
1218
#define LKDBCheck_tableNameIsInvalid(tableName) \
1319
if ([LKDBUtils checkStringIsEmpty:tableName]) { \
1420
LKErrorLog(@" \n Fail!Fail!Fail!Fail! \n with TableName is nil"); \
@@ -101,26 +107,32 @@ + (LKDBHelper *)dbHelperWithPath:(NSString *)dbFilePath save:(LKDBHelper *)helpe
101107
{
102108
NSMutableArray *dbArray = [self dbHelperSingleArray];
103109
LKDBHelper *instance = nil;
104-
@synchronized(dbArray)
105-
{
106-
if (helper) {
107-
LKDBWeakObject *weakObj = [[LKDBWeakObject alloc] init];
108-
weakObj.obj = helper;
109-
[dbArray addObject:weakObj];
110-
} else if (dbFilePath) {
111-
for (NSInteger i = 0; i < dbArray.count;) {
112-
LKDBWeakObject *weakObj = [dbArray objectAtIndex:i];
113-
if (weakObj.obj == nil) {
114-
[dbArray removeObjectAtIndex:i];
115-
continue;
116-
} else if ([weakObj.obj.dbPath isEqualToString:dbFilePath]) {
117-
instance = weakObj.obj;
118-
break;
119-
}
120-
i++;
110+
dbFilePath = dbFilePath.lowercaseString;
111+
112+
NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
113+
BOOL hasCached = NO;
114+
115+
for (NSInteger i = 0; i < dbArray.count; i++) {
116+
LKDBWeakObject *weakObj = [dbArray objectAtIndex:i];
117+
if ([weakObj.obj.dbPath.lowercaseString isEqualToString:dbFilePath]) {
118+
if (helper) {
119+
hasCached = YES;
120+
} else {
121+
instance = weakObj.obj;
121122
}
123+
} else if (!weakObj.obj){
124+
[indexSet addIndex:i];
122125
}
123126
}
127+
128+
[dbArray removeObjectsAtIndexes:indexSet];
129+
130+
if (!hasCached && helper) {
131+
LKDBWeakObject *weakObj = [[LKDBWeakObject alloc] init];
132+
weakObj.obj = helper;
133+
[dbArray addObject:weakObj];
134+
}
135+
124136
return instance;
125137
}
126138

@@ -141,23 +153,21 @@ - (instancetype)initWithDBPath:(NSString *)filePath
141153
self = nil;
142154
return nil;
143155
}
144-
145-
LKDBHelper *helper = [LKDBHelper dbHelperWithPath:filePath save:nil];
146-
147-
if (helper) {
148-
self = helper;
149-
} else {
150-
self = [super init];
151-
152-
if (self) {
153-
self.threadLock = [[NSRecursiveLock alloc] init];
154-
self.createdTableNames = [NSMutableArray array];
155-
156-
[self setDBPath:filePath];
157-
[LKDBHelper dbHelperWithPath:nil save:self];
156+
@synchronized([LKDBHelper class]) {
157+
LKDBHelper *helper = [LKDBHelper dbHelperWithPath:filePath save:nil];
158+
if (helper) {
159+
self = helper;
160+
} else {
161+
self = [super init];
162+
if (self) {
163+
self.threadLock = [[NSRecursiveLock alloc] init];
164+
self.createdTableNames = [NSMutableArray array];
165+
166+
[self setDBPath:filePath];
167+
[LKDBHelper dbHelperWithPath:nil save:self];
168+
}
158169
}
159170
}
160-
161171
return self;
162172
}
163173

@@ -227,17 +237,9 @@ - (void)setDBPath:(NSString *)filePath
227237
self.dbPath = filePath;
228238
[self.bindingQueue close];
229239
[self.createdTableNames removeAllObjects];
230-
231-
#ifndef SQLITE_OPEN_FILEPROTECTION_NONE
232-
#define SQLITE_OPEN_FILEPROTECTION_NONE 0x00400000
233-
#endif
240+
234241
self.bindingQueue = [[FMDatabaseQueue alloc] initWithPath:filePath
235-
flags:SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FILEPROTECTION_NONE];
236-
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
237-
if ([fileManager fileExistsAtPath:filePath]) {
238-
[fileManager setAttributes:@{ NSFileProtectionKey: NSFileProtectionNone } ofItemAtPath:filePath error:nil];
239-
}
240-
#endif
242+
flags:LKDBOpenFlags];
241243

242244
///reset encryptionKey
243245
_encryptionKey = nil;
@@ -248,6 +250,13 @@ - (void)setDBPath:(NSString *)filePath
248250
[self.threadLock unlock];
249251
}
250252

253+
- (void)closeDB {
254+
[self.threadLock lock];
255+
[self.bindingQueue close];
256+
self.bindingQueue = nil;
257+
[self.threadLock unlock];
258+
}
259+
251260
#pragma mark - core
252261
- (void)executeDB:(void (^)(FMDatabase *db))block
253262
{
@@ -261,7 +270,8 @@ - (void)executeDB:(void (^)(FMDatabase *db))block
261270
block(self.usingdb);
262271
} else {
263272
if (self.bindingQueue == nil) {
264-
self.bindingQueue = [[FMDatabaseQueue alloc] initWithPath:_dbPath];
273+
self.bindingQueue = [[FMDatabaseQueue alloc] initWithPath:self.dbPath
274+
flags:LKDBOpenFlags];
265275
[self.createdTableNames removeAllObjects];
266276
[self.bindingQueue inDatabase:^(FMDatabase *db) {
267277
db.logsErrors = LKDBLogErrorEnable;

0 commit comments

Comments
 (0)