@@ -168,6 +168,7 @@ - (instancetype)initWithDBPath:(NSString *)filePath
168
168
self.threadLock = [[NSRecursiveLock alloc ] init ];
169
169
self.createdTableNames = [NSMutableArray array ];
170
170
self.lastExecuteDBTime = [[NSDate date ] timeIntervalSince1970 ];
171
+ self.autoCloseDBDelayTime = 15 ;
171
172
172
173
[self setDBPath: filePath];
173
174
[LKDBHelper dbHelperWithPath: nil save: self ];
@@ -199,68 +200,43 @@ - (void)setDBName:(NSString *)dbName
199
200
200
201
- (void )setDBPath : (NSString *)filePath
201
202
{
203
+ [self .threadLock lock ];
202
204
if (self.bindingQueue && [self .dbPath isEqualToString: filePath]) {
203
- return ;
204
- }
205
- NSFileManager *fileManager = [NSFileManager defaultManager ];
206
- // 创建数据库目录
207
- NSRange lastComponent = [filePath rangeOfString: @" /" options: NSBackwardsSearch];
208
-
209
- if (lastComponent.length > 0 ) {
210
- NSString *dirPath = [filePath substringToIndex: lastComponent.location];
211
- BOOL isDir = NO ;
212
- BOOL isCreated = [fileManager fileExistsAtPath: dirPath isDirectory: &isDir];
213
-
214
- if ((isCreated == NO ) || (isDir == NO )) {
215
- NSError *error = nil ;
216
- #ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
217
- NSDictionary *attributes = @{NSFileProtectionKey : NSFileProtectionNone };
218
- #else
219
- NSDictionary *attributes = nil ;
220
- #endif
221
- BOOL success = [fileManager createDirectoryAtPath: dirPath
222
- withIntermediateDirectories: YES
223
- attributes: attributes
224
- error: &error];
225
- if (success == NO ) {
226
- LKErrorLog (@" create dir error: %@ " , error.debugDescription );
227
- }
228
- } else {
229
- /* *
230
- * @brief Disk I/O error when device is locked
231
- * https://github.com/ccgus/fmdb/issues/262
232
- */
233
- #ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
234
- [fileManager setAttributes: @{ NSFileProtectionKey : NSFileProtectionNone }
235
- ofItemAtPath: dirPath
236
- error: nil ];
237
- #endif
238
- }
205
+ LKErrorLog (@" current dbPath isEqual filePath :%@ " , filePath);
206
+ } else {
207
+ // reset encryptionKey
208
+ _encryptionKey = nil ;
209
+ // set db path
210
+ self.dbPath = filePath;
211
+ [self openDB ];
239
212
}
213
+ [self .threadLock unlock ];
214
+ }
240
215
241
- [self .threadLock lock ];
242
-
243
- self.dbPath = filePath;
216
+ - (void )openDB {
217
+ // / 重置所有配置
244
218
[self .bindingQueue close ];
245
219
[self .createdTableNames removeAllObjects ];
246
220
221
+ NSString *filePath = self.dbPath ;
222
+ BOOL hasCreated = [LKDBUtils createDirectoryWithFilePath: filePath];
223
+ if (!hasCreated) {
224
+ // / 数据库目录创建失败
225
+ return ;
226
+ }
227
+
247
228
self.bindingQueue = [[FMDatabaseQueue alloc ] initWithPath: filePath
248
229
flags: LKDBOpenFlags];
249
-
250
- // /reset encryptionKey
251
- _encryptionKey = nil ;
252
-
253
230
[self .bindingQueue inDatabase: ^(FMDatabase *db) {
254
231
db.logsErrors = LKDBLogErrorEnable;
255
232
}];
256
233
257
234
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
235
+ NSFileManager *fileManager = [NSFileManager defaultManager ];
258
236
if ([fileManager fileExistsAtPath: filePath]) {
259
237
[fileManager setAttributes: @{ NSFileProtectionKey : NSFileProtectionNone } ofItemAtPath: filePath error: nil ];
260
238
}
261
239
#endif
262
-
263
- [self .threadLock unlock ];
264
240
}
265
241
266
242
- (void )closeDB {
@@ -283,15 +259,12 @@ - (void)executeDB:(void (^)(FMDatabase *db))block
283
259
block (self.usingdb );
284
260
} else {
285
261
if (self.bindingQueue == nil ) {
286
- [self .createdTableNames removeAllObjects ];
287
- self.bindingQueue = [[FMDatabaseQueue alloc ] initWithPath: self .dbPath
288
- flags: LKDBOpenFlags];
289
- [self .bindingQueue inDatabase: ^(FMDatabase *db) {
290
- db.logsErrors = LKDBLogErrorEnable;
291
- if (_encryptionKey.length > 0 ) {
262
+ [self openDB ];
263
+ if (_encryptionKey.length > 0 ) {
264
+ [self .bindingQueue inDatabase: ^(FMDatabase *db) {
292
265
[db setKey: _encryptionKey];
293
- }
294
- }];
266
+ }];
267
+ }
295
268
}
296
269
[self .bindingQueue inDatabase: ^(FMDatabase *db) {
297
270
self.usingdb = db;
@@ -339,7 +312,8 @@ - (void)startAutoCloseTimer {
339
312
340
313
- (BOOL )autoCloseDBConnection {
341
314
NSInteger now = [[NSDate date ] timeIntervalSince1970 ];
342
- if (now - self.lastExecuteDBTime > 5 ) {
315
+ // / 如果10秒没有操作 则关闭数据库链接
316
+ if (now - self.lastExecuteDBTime > 10 ) {
343
317
[self closeDB ];
344
318
return YES ;
345
319
}
@@ -395,7 +369,7 @@ - (void)executeForTransaction:(BOOL (^)(LKDBHelper *))block
395
369
LKDBHelper *helper = self;
396
370
397
371
[self executeDB: ^(FMDatabase *db) {
398
- BOOL inTransacttion = db.inTransaction ;
372
+ BOOL inTransacttion = db.isInTransaction ;
399
373
400
374
if (!inTransacttion) {
401
375
[db beginTransaction ];
0 commit comments