FMDatabaseQueue Class Reference
| Inherits from | NSObject |
| Declared in | FMDatabaseQueue.h FMDatabaseQueue.m |
Overview
To perform queries and updates on multiple threads, you’ll want to use FMDatabaseQueue.
Using a single instance of FMDatabase from multiple threads at once is a bad idea. It has always been OK to make a FMDatabase object per thread. Just don’t share a single instance across threads, and definitely not across multiple threads at the same time.
Instead, use FMDatabaseQueue. Here’s how to use it:
First, make your queue.
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:aPath];
Then use it like so:
[queue inDatabase:^(FMDatabase *db) {
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:1]];
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:2]];
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:3]];
FMResultSet *rs = [db executeQuery:@"select * from foo"];
while ([rs next]) {
//…
}
}];
An easy way to wrap things up in a transaction can be done like this:
[queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:1]];
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:2]];
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:3]];
if (whoopsSomethingWrongHappened) {
*rollback = YES;
return;
}
// etc…
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:4]];
}];
FMDatabaseQueue will run the blocks on a serialized queue (hence the name of the class). So if you call FMDatabaseQueue’s methods from multiple threads at the same time, they will be executed in the order they are received. This way queries and updates won’t step on each other’s toes, and every one is happy.
See also
Warning: Do not instantiate a single FMDatabase object and use it across multiple threads. Use FMDatabaseQueue instead.
Warning: The calls to FMDatabaseQueue’s methods are blocking. So even though you are passing along blocks, they will not be run on another thread.
Tasks
Initialization, opening, and closing of queue
-
+ databaseQueueWithPath:Create queue using path.
-
– initWithPath:Create queue using path.
-
– closeClose database used by queue.
Dispatching database operations to queue
-
– inDatabase:Synchronously perform database operations on queue.
-
– inTransaction:Synchronously perform database operations on queue, using transactions.
-
– inDeferredTransaction:Synchronously perform database operations on queue, using deferred transactions.
-
– inSavePoint:Synchronously perform database operations using save point.
Instance Methods
inDatabase:
Synchronously perform database operations on queue.
- (void)inDatabase:(void ( ^ ) ( FMDatabase *db ))blockParameters
- block
The code to be run on the queue of
FMDatabaseQueue
Declared In
FMDatabaseQueue.hinDeferredTransaction:
Synchronously perform database operations on queue, using deferred transactions.
- (void)inDeferredTransaction:(void ( ^ ) ( FMDatabase *db , BOOL *rollback ))blockParameters
- block
The code to be run on the queue of
FMDatabaseQueue
Declared In
FMDatabaseQueue.hinSavePoint:
Synchronously perform database operations using save point.
- (NSError *)inSavePoint:(void ( ^ ) ( FMDatabase *db , BOOL *rollback ))blockParameters
- block
The code to be run on the queue of
FMDatabaseQueue
Declared In
FMDatabaseQueue.h