Skip to content

Commit 399cce0

Browse files
抽取检测文件夹是否创建的方法,默认自动关闭数据库连接时间为15秒
1 parent 2d61037 commit 399cce0

File tree

3 files changed

+83
-55
lines changed

3 files changed

+83
-55
lines changed

LKDBHelper/Helper/LKDBHelper.m

Lines changed: 29 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ - (instancetype)initWithDBPath:(NSString *)filePath
168168
self.threadLock = [[NSRecursiveLock alloc] init];
169169
self.createdTableNames = [NSMutableArray array];
170170
self.lastExecuteDBTime = [[NSDate date] timeIntervalSince1970];
171+
self.autoCloseDBDelayTime = 15;
171172

172173
[self setDBPath:filePath];
173174
[LKDBHelper dbHelperWithPath:nil save:self];
@@ -199,68 +200,43 @@ - (void)setDBName:(NSString *)dbName
199200

200201
- (void)setDBPath:(NSString *)filePath
201202
{
203+
[self.threadLock lock];
202204
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];
239212
}
213+
[self.threadLock unlock];
214+
}
240215

241-
[self.threadLock lock];
242-
243-
self.dbPath = filePath;
216+
- (void)openDB {
217+
/// 重置所有配置
244218
[self.bindingQueue close];
245219
[self.createdTableNames removeAllObjects];
246220

221+
NSString *filePath = self.dbPath;
222+
BOOL hasCreated = [LKDBUtils createDirectoryWithFilePath:filePath];
223+
if (!hasCreated) {
224+
/// 数据库目录创建失败
225+
return;
226+
}
227+
247228
self.bindingQueue = [[FMDatabaseQueue alloc] initWithPath:filePath
248229
flags:LKDBOpenFlags];
249-
250-
///reset encryptionKey
251-
_encryptionKey = nil;
252-
253230
[self.bindingQueue inDatabase:^(FMDatabase *db) {
254231
db.logsErrors = LKDBLogErrorEnable;
255232
}];
256233

257234
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
235+
NSFileManager *fileManager = [NSFileManager defaultManager];
258236
if ([fileManager fileExistsAtPath:filePath]) {
259237
[fileManager setAttributes:@{ NSFileProtectionKey: NSFileProtectionNone } ofItemAtPath:filePath error:nil];
260238
}
261239
#endif
262-
263-
[self.threadLock unlock];
264240
}
265241

266242
- (void)closeDB {
@@ -283,15 +259,12 @@ - (void)executeDB:(void (^)(FMDatabase *db))block
283259
block(self.usingdb);
284260
} else {
285261
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) {
292265
[db setKey:_encryptionKey];
293-
}
294-
}];
266+
}];
267+
}
295268
}
296269
[self.bindingQueue inDatabase:^(FMDatabase *db) {
297270
self.usingdb = db;
@@ -339,7 +312,8 @@ - (void)startAutoCloseTimer {
339312

340313
- (BOOL)autoCloseDBConnection {
341314
NSInteger now = [[NSDate date] timeIntervalSince1970];
342-
if (now - self.lastExecuteDBTime > 5) {
315+
/// 如果10秒没有操作 则关闭数据库链接
316+
if (now - self.lastExecuteDBTime > 10) {
343317
[self closeDB];
344318
return YES;
345319
}
@@ -395,7 +369,7 @@ - (void)executeForTransaction:(BOOL (^)(LKDBHelper *))block
395369
LKDBHelper *helper = self;
396370

397371
[self executeDB:^(FMDatabase *db) {
398-
BOOL inTransacttion = db.inTransaction;
372+
BOOL inTransacttion = db.isInTransaction;
399373

400374
if (!inTransacttion) {
401375
[db beginTransaction];

LKDBHelper/Helper/LKDBUtils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
NS_ASSUME_NONNULL_BEGIN
1212

1313
@interface LKDBUtils : NSObject
14+
15+
// 创建文件路径所需要的文件夹
16+
+ (BOOL)createDirectoryWithFilePath:(NSString *)filePath;
17+
1418
///返回根目录路径 "document"
1519
+ (NSString *)getDocumentPath;
1620
///返回 "document/dir/" 文件夹路径

LKDBHelper/Helper/LKDBUtils.m

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,56 @@ - (NSNumber *)numberFromString:(NSString *)string
7171
@end
7272

7373
@implementation LKDBUtils
74+
75+
+ (BOOL)createDirectoryWithFilePath:(NSString *)filePath
76+
{
77+
NSString *dirPath = filePath.stringByDeletingLastPathComponent;
78+
if (!dirPath) {
79+
return NO;
80+
}
81+
82+
NSFileManager *fileManager = [NSFileManager defaultManager];
83+
84+
BOOL isDir = NO;
85+
BOOL isCreated = [fileManager fileExistsAtPath:dirPath isDirectory:&isDir];
86+
87+
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
88+
NSDictionary *attributes = @{NSFileProtectionKey: NSFileProtectionNone};
89+
#else
90+
NSDictionary *attributes = nil;
91+
#endif
92+
93+
if (!isCreated || !isDir) {
94+
NSError *error = nil;
95+
BOOL success = [fileManager createDirectoryAtPath:dirPath
96+
withIntermediateDirectories:YES
97+
attributes:attributes
98+
error:&error];
99+
if (!success) {
100+
LKErrorLog(@"create dir error: %@", error.debugDescription);
101+
/// 下个主线程继续尝试次
102+
dispatch_async(dispatch_get_main_queue(), ^{
103+
[fileManager createDirectoryAtPath:dirPath
104+
withIntermediateDirectories:YES
105+
attributes:attributes
106+
error:nil];
107+
});
108+
}
109+
return success;
110+
} else {
111+
/**
112+
* @brief Disk I/O error when device is locked
113+
* https://github.com/ccgus/fmdb/issues/262
114+
*/
115+
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
116+
[fileManager setAttributes:attributes
117+
ofItemAtPath:dirPath
118+
error:nil];
119+
#endif
120+
return YES;
121+
}
122+
}
123+
74124
+ (NSString *)getDocumentPath
75125
{
76126
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED

0 commit comments

Comments
 (0)