Browse Source

Refine close logic

- If you call `[db close]`, it should reset `isOpen` to false;
- When you try to use `[queue database]`, it should not only check that the `FMDatabase` is non-nil, but also whether the database is open or not;
- Added `testClose` test to `FMDatabaseQueue` logic if the queue's `FMDatabase` was closed.
Robert M. Ryan 8 năm trước cách đây
mục cha
commit
b9c456867b
3 tập tin đã thay đổi với 21 bổ sung2 xóa
  1. 15 0
      Tests/FMDatabaseQueueTests.m
  2. 2 0
      src/fmdb/FMDatabase.m
  3. 4 2
      src/fmdb/FMDatabaseQueue.m

+ 15 - 0
Tests/FMDatabaseQueueTests.m

@@ -354,4 +354,19 @@ - (void)testSavePoint
     
 }
 
+- (void)testClose
+{
+    [self.queue inDatabase:^(FMDatabase *adb) {
+        XCTAssertTrue([adb executeUpdate:@"CREATE TABLE close_test (a INTEGER)"]);
+        XCTAssertTrue([adb executeUpdate:@"INSERT INTO close_test VALUES (1)"]);
+        
+        [adb close];
+    }];
+    
+    [self.queue inDatabase:^(FMDatabase *adb) {
+        FMResultSet *ars = [adb executeQuery:@"select * from close_test"];
+        XCTAssertNotNil(ars);
+    }];
+}
+
 @end

+ 2 - 0
src/fmdb/FMDatabase.m

@@ -261,6 +261,8 @@ - (BOOL)close {
     while (retry);
     
     _db = nil;
+    _isOpen = false;
+    
     return YES;
 }
 

+ 4 - 2
src/fmdb/FMDatabaseQueue.m

@@ -156,8 +156,10 @@ - (void)interrupt {
 }
 
 - (FMDatabase*)database {
-    if (!_db) {
-       _db = FMDBReturnRetained([[[self class] databaseClass] databaseWithPath:_path]);
+    if (![_db isOpen]) {
+        if (!_db) {
+           _db = FMDBReturnRetained([[[self class] databaseClass] databaseWithPath:_path]);
+        }
         
 #if SQLITE_VERSION_NUMBER >= 3005000
         BOOL success = [_db openWithFlags:_openFlags vfs:_vfsName];