AWSFMDatabaseQueue

Objective-C

@interface AWSFMDatabaseQueue : NSObject {
  NSString *_path;
  dispatch_queue_t _queue;
  AWSFMDatabase *_db;
  int _openFlags;
}

Swift

class AWSFMDatabaseQueue : NSObject

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

  • <FMDatabase>

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.

  • Undocumented

    Declaration

    Objective-C

    NSString            *_path
  • Undocumented

    Declaration

    Objective-C

    dispatch_queue_t    _queue
  • _db

    Undocumented

    Declaration

    Objective-C

    AWSFMDatabase          *_db
  • Undocumented

    Declaration

    Objective-C

    int                 _openFlags
  • Path of database

    Declaration

    Objective-C

    @property (retain) NSString *path;

    Swift

    var path: String! { get set }
  • Open flags

    Declaration

    Objective-C

    @property (readonly) int openFlags;

    Swift

    var openFlags: Int32 { get }

Initialization, opening, and closing of queue

  • Create queue using path.

    Declaration

    Objective-C

    + (instancetype)databaseQueueWithPath:(NSString *)aPath;

    Parameters

    aPath

    The file path of the database.

    Return Value

    The FMDatabaseQueue object. nil on error.

  • Create queue using path and specified flags.

    Declaration

    Objective-C

    + (instancetype)databaseQueueWithPath:(NSString *)aPath flags:(int)openFlags;

    Parameters

    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.

  • Create queue using path.

    Declaration

    Objective-C

    - (instancetype)initWithPath:(NSString *)aPath;

    Swift

    init!(path aPath: String!)

    Parameters

    aPath

    The file path of the database.

    Return Value

    The FMDatabaseQueue object. nil on error.

  • Create queue using path and specified flags.

    Declaration

    Objective-C

    - (instancetype)initWithPath:(NSString *)aPath flags:(int)openFlags;

    Swift

    init!(path aPath: String!, flags openFlags: Int32)

    Parameters

    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.

  • Create queue using path and specified flags.

    Declaration

    Objective-C

    - (instancetype)initWithPath:(NSString *)aPath
                           flags:(int)openFlags
                             vfs:(NSString *)vfsName;

    Swift

    init!(path aPath: String!, flags openFlags: Int32, vfs vfsName: String!)

    Parameters

    aPath

    The file path of the database.

    openFlags

    Flags passed to the openWithFlags method of the database

    vfsName

    The name of a custom virtual file system

    Return Value

    The FMDatabaseQueue object. nil on error.

  • Returns the Class of ‘FMDatabase’ subclass, that will be used to instantiate database object.

    Subclasses can override this method to return specified Class of ‘FMDatabase’ subclass.

    Declaration

    Objective-C

    + (Class)databaseClass;

    Swift

    class func databaseClass() -> AnyClass!

    Return Value

    The Class of ‘FMDatabase’ subclass, that will be used to instantiate database object.

  • Close database used by queue.

    Declaration

    Objective-C

    - (void)close;

    Swift

    func close()

Dispatching database operations to queue

  • Synchronously perform database operations on queue.

    Declaration

    Objective-C

    - (void)inDatabase:(void (^)(AWSFMDatabase *))block;

    Swift

    func inDatabase(_ block: ((AWSFMDatabase?) -> Void)!)

    Parameters

    block

    The code to be run on the queue of FMDatabaseQueue

  • Synchronously perform database operations on queue, using transactions.

    Declaration

    Objective-C

    - (void)inTransaction:(void (^)(AWSFMDatabase *, BOOL *))block;

    Swift

    func inTransaction(_ block: ((AWSFMDatabase?, UnsafeMutablePointer<ObjCBool>?) -> Void)!)

    Parameters

    block

    The code to be run on the queue of FMDatabaseQueue

  • Synchronously perform database operations on queue, using deferred transactions.

    Declaration

    Objective-C

    - (void)inDeferredTransaction:(void (^)(AWSFMDatabase *, BOOL *))block;

    Swift

    func inDeferredTransaction(_ block: ((AWSFMDatabase?, UnsafeMutablePointer<ObjCBool>?) -> Void)!)

    Parameters

    block

    The code to be run on the queue of FMDatabaseQueue

  • Synchronously perform database operations using save point.

    Declaration

    Objective-C

    - (NSError *)inSavePoint:(void (^)(AWSFMDatabase *, BOOL *))block;

    Swift

    func inSavePoint(_ block: ((AWSFMDatabase?, UnsafeMutablePointer<ObjCBool>?) -> Void)!) -> Error!

    Parameters

    block

    The code to be run on the queue of FMDatabaseQueue

AWSHelpers

  • Convenience method to open a database queue with the SQLITE_OPEN_FULLMUTEX flag so it can be safely accessed across threads.

    Declaration

    Objective-C

    + (nonnull instancetype)serialDatabaseQueueWithPath:(nonnull NSString *)aPath;

    Swift

    class func serialDatabaseQueue(withPath aPath: String) -> Self

    Parameters

    aPath

    The file path of the database.

    Return Value

    The FMDatabaseQueue object. nil on error.