SQLite Database can work on secondary dispatch async thread

75 views Asked by At

Hello Any one have idea or code to manage sqlite database working on dispatch async task in background thread without any issue such as database locked issue.

1

There are 1 answers

0
Ratul Sharker On

hope you're using a class xyzDBMgr to execute all the query on a xyz.sqlite file. say you're using foo1, foo2 methods. Use @synchronized block upon self to lock the table and ensure thread safety.

xyzDBMgr.m

@implementation xyzDBMgr


void foo1
{
     @synchronized(self)
     {
         // your locked code goes here
     }
}

void foo2
{
     @synchronized(self)
     {
        // your locked code goes here
     }
}

...
@end

Do not call lock function inside of another lock function. In case you called foo1 inside from the foo2 then a deadlock is obvious.

If you want to call these method from secondary thread you can use apple's GCD api calls like dispathc_async or NSOperationQueue api calls.