Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asynchronously perform database operations on queue, using transactions. #469

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/fmdb/FMDatabaseQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@

- (void)inTransaction:(void (^)(FMDatabase *db, BOOL *rollback))block;

/** Asynchronously perform database operations on queue, using transactions.

@param block The code to be run on the queue of `FMDatabaseQueue`
*/

- (void)inTransactionAsync:(void (^)(FMDatabase *db, BOOL *rollback))block;

/** Synchronously perform database operations on queue, using deferred transactions.

@param block The code to be run on the queue of `FMDatabaseQueue`
Expand Down
31 changes: 31 additions & 0 deletions src/fmdb/FMDatabaseQueue.m
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,37 @@ - (void)inTransaction:(void (^)(FMDatabase *db, BOOL *rollback))block {
[self beginTransaction:NO withBlock:block];
}

- (void)inTransactionAsync:(void (^)(FMDatabase *db, BOOL *rollback))block{
[self beginTransactionAsync:NO withBlock:block];
}

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

dispatch_async(_queue, ^() {
FMDBRetain(self);
BOOL shouldRollback = NO;

if (useDeferred) {
[[self database] beginDeferredTransaction];
}
else {
[[self database] beginTransaction];
}

block([self database], &shouldRollback);

if (shouldRollback) {
[[self database] rollback];
}
else {
[[self database] commit];
}
FMDBRelease(self);
});

}


- (NSError*)inSavePoint:(void (^)(FMDatabase *db, BOOL *rollback))block {
#if SQLITE_VERSION_NUMBER >= 3007000
static unsigned long savePointIdx = 0;
Expand Down