Friday, 12 December 2014

SQLite3 thread safe execution in iOS


 
You can use lock (such as those enumerated in the Synchronization section of the Threading Programming Guide) or you can use a dedicated serial queue. For example, create a queue:
@property (nonatomic, strong) dispatch_queue_t databaseQueue;
Instantiate it:
self.databaseQueue = dispatch_queue_create("com.company.app.database", 0);
And whenever you want to interact with the database, you can do
dispatch_sync(self.databaseQueue, ^(void){
    // do your database activity here
});
If you want to simplify your life, the FMDB library has a FMDatabaseQueue object that does much of this for you (as well as greatly simplifying your database interaction in general).

for more detail read http://stackoverflow.com/questions/20029782/how-to-handle-multiple-thread-access-the-sqlite3-with-out-dblocked-error

No comments:

Post a Comment