Question

I have some code using the FMDB sqllite wrapper (https://github.com/ccgus/fmdb) and I'm expecting this code to be executed from multiple threads. As such, I'm using the FMDatabaseQueue class to execute all DB work.

I'm a little bit confused by the pattern, e.g.

FMDatabaseQueue *q = [FMDatabaseQueue databaseQueueWithPath:@""];
[q inDatabase:^(FMDatabase *db) {
    NSLog(@"1");
    BOOL yn = [db executeUpdate:@"CREATE TABLE IF NOT EXISTS Foo (Bar TEXT)"];
    NSLog(@"2%@", yn ? @"YES" : @"NO");
}];
NSLog(@"3");

If you look at the code above.. is it guaranteed that 3 will always be logged after 2 and 1? Such that the executing thread is blocked until the block provided to the queue actually gets to execute?

If so, should I remove any non-database work outside and after the block so that any other threads using the queue are not blocked by work that doesn't need to be synchronized across threads?

Also, do I need to call [db open] when using FMDatabaseQueue?

Was it helpful?

Solution

Yes, it's guaranteed that 3 will be logged after 1 and 2.

That's because FMDatabaseQueue -inDatabase: uses a dispatch_sync call internally to do the database operations (as you can see in the source code).

So, the recommendation would be do the less work possible inside the block, and do all the other work on your own queue.

Also, [db open] is not needed, because FMDatabaseQueue -initWithPath: (called by FMDatabaseQueue +databaseQueueWithPath:) already does that (as again, you can see in the source code).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top