Search code examples
iossqlitebackground-processfmdb

ios - perform database actions on background thread with FMDB


I'm working with a local SQLITE db in an iOS (obj-c) project where we've chosen to use FMDB. Please note I'm not interested in changing to Core Data or switching from FMDB to a different library.

Everything is working, but when we first run the app data is downloaded from our server, json is parsed, and then sorted into the local db. Due to the amount of info coming in this takes a good 30 - 40 seconds. Ideally this would be done in the background, but FMDB does not allow for this (at least as far as I can tell) as it may cause issues with the db data integrity.

Has anyone successfully used the FMDB library running all queries, inserts, etc on a background thread? I've tried simply wrapping actions in:

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // ... query/insert/update/delete
    });

...but I end up with db integrity issues. Any tips or examples would be great. Thanks in advance.


Solution

  • @nawar - Sorry for not posting this sooner. I ended up using a single instace of FMDatabaseQueue (instead of FMDatabase) within the dispatch_async block. Works like a charm. The issues I was running into were due to using FMDatabase.