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
Other Methods
-
+ databaseQueueWithPath:Create queue using path.
-
+ databaseQueueWithPath:flags:Create queue using path and specified flags.
-
+ databaseClassReturns the Class of ‘FMDatabase’ subclass, that will be used to instantiate database object.
-
– initWithPath:flags:Create queue using path and specified flags.
-
– initWithPath:Create queue using path.
-
– closeClose database used by queue.
-
– inDatabase:Synchronously perform database operations on queue.
-
– inDeferredTransaction:Synchronously perform database operations on queue, using deferred transactions.
-
– inTransaction:Synchronously perform database operations on queue, using transactions.
-
– inSavePoint:Synchronously perform database operations using save point.
Other Methods
Class Methods
databaseClass
Returns the Class of ‘FMDatabase’ subclass, that will be used to instantiate database object.
+ (Class)databaseClassReturn Value
The Class of ‘FMDatabase’ subclass, that will be used to instantiate database object.
Discussion
Subclasses can override this method to return specified Class of ‘FMDatabase’ subclass.
Declared In
FMDatabaseQueue.hdatabaseQueueWithPath:
Create queue using path.
+ (instancetype)databaseQueueWithPath:(NSString *)aPathParameters
- aPath
The file path of the database.
Return Value
The FMDatabaseQueue object. nil on error.
Declared In
FMDatabaseQueue.hdatabaseQueueWithPath:flags:
Create queue using path and specified flags.
+ (instancetype)databaseQueueWithPath:(NSString *)aPath flags:(int)openFlagsParameters
- aPath
The file path of the database.
- openFlags
Flags passed to the openWithFlags method of the database
Return Value
The FMDatabaseQueue object. nil on error.
Declared In
FMDatabaseQueue.hInstance 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.hinTransaction:
Synchronously perform database operations on queue, using transactions.
- (void)inTransaction:(void ( ^ ) ( FMDatabase *db , BOOL *rollback ))blockParameters
- block
The code to be run on the queue of
FMDatabaseQueue
Declared In
FMDatabaseQueue.hinitWithPath:
Create queue using path.
- (instancetype)initWithPath:(NSString *)aPathParameters
- aPath
The file path of the database.
Return Value
The FMDatabaseQueue object. nil on error.
Declared In
FMDatabaseQueue.hinitWithPath:flags:
Create queue using path and specified flags.
- (instancetype)initWithPath:(NSString *)aPath flags:(int)openFlagsParameters
- aPath
The file path of the database.
- openFlags
Flags passed to the openWithFlags method of the database
Return Value
The FMDatabaseQueue object. nil on error.
Declared In
FMDatabaseQueue.h