Skip to content

Commit

Permalink
"Unlike with dispatch_async, no retain is performed on the target queue"
Browse files Browse the repository at this point in the history
  • Loading branch information
ccgus committed Feb 7, 2012
1 parent 423fbc7 commit 8d63f4d
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/FMDatabaseQueue.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#import "FMDatabaseQueue.h"
#import "FMDatabase.h"

// Note: we call [self retain]; before using dispatch_sync, just incase FMDatabaseQueue is released on another thread and we're in the middle of doing something in dispatch_sync

@implementation FMDatabaseQueue

@synthesize path = _path;
Expand Down Expand Up @@ -60,11 +62,13 @@ - (void)dealloc {
}

- (void)close {
[self retain];
dispatch_sync(_queue, ^() {
[_db close];
FMDBRelease(_db);
_db = 0x00;
});
[self release];
}

- (FMDatabase*)database {
Expand All @@ -83,6 +87,7 @@ - (FMDatabase*)database {
}

- (void)inDatabase:(void (^)(FMDatabase *db))block {
[self retain];

dispatch_sync(_queue, ^() {

Expand All @@ -92,12 +97,13 @@ - (void)inDatabase:(void (^)(FMDatabase *db))block {
if ([db hasOpenResultSets]) {
NSLog(@"Warning: there is at least one open result set around after performing [FMDatabaseQueue inDatabase:]");
}

});

[self release];
}

- (void)beginTransaction:(BOOL)useDeferred withBlock:(void (^)(FMDatabase *db, BOOL *rollback))block {

[self retain];
dispatch_sync(_queue, ^() {

BOOL shouldRollback = NO;
Expand All @@ -117,8 +123,9 @@ - (void)beginTransaction:(BOOL)useDeferred withBlock:(void (^)(FMDatabase *db, B
else {
[[self database] commit];
}

});

[self release];
}

- (void)inDeferredTransaction:(void (^)(FMDatabase *db, BOOL *rollback))block {
Expand All @@ -134,7 +141,7 @@ - (NSError*)inSavePoint:(void (^)(FMDatabase *db, BOOL *rollback))block {

static unsigned long savePointIdx = 0;
__block NSError *err = 0x00;

[self retain];
dispatch_sync(_queue, ^() {

NSString *name = [NSString stringWithFormat:@"savePoint%ld", savePointIdx++];
Expand All @@ -154,7 +161,7 @@ - (NSError*)inSavePoint:(void (^)(FMDatabase *db, BOOL *rollback))block {

}
});

[self release];
return err;
}
#endif
Expand Down

0 comments on commit 8d63f4d

Please sign in to comment.