Sqlite3 multithreading in c lens

I run a background thread in my application with dispatch_async, and several times my main thread and background thread accessed the database at the same time, and this gave me a database error, and I tried to solve it using sqlite3_threadsafe (), which was always returning 2. those. I cannot use the same database connection in two threads and I want it to return 1 can someone help me in this regard

+3
source share
2 answers

I think you are pursuing the wrong approach. SQLite is not thread safe, no matter how you compile it - see the FAQ . Even if SQLite is compiled to ensure thread safety, the same database cannot be used from multiple threads if any transactions are expected or any statements remain incomplete.

The recommendations above to use Grand Central Dispatch to redirect all SQLite queries to the sequential send sequence are the correct way to act in my opinion, although I would be more likely to recommend you create your own queue rather than using the main queue for that simple the reason is that you can reliably send dispatch_sync when you want to wait for the result.

+4

, sqlite.

dispatch_async(dispatch_get_main_queue(), ^ {
    //code goes here
});
+1

All Articles