My Sqlite Database is accessed from various threads in my code due to which I am getting lot of Database LOCK issue.
To overcome the lock issue I am querying the DB in main thread only. The drawback of this is my main thread is busy if there are lot of queries to be executed.
I want to write a mechanism wherein all the DB calls are executed from one place (in a secondary thread) so that it can be synchronized and the caller can call from any thread.
It may be like if any module wants to execute a query it will check from the main DB Sync class whether the DB is busy or free to perform the task.
Note:- In my current implementation I am opening and closing the DB every time I want to execute the query. Will it have any performance impact ?
Any Hint in right Direction would be highly appreciated.
Reopening the database will of course take more time than keeping it open. However, it might not be a noticeable amount of time.
I suggest you create a Grand Central Dispatch queue and use it for all database access. Here’s how you create it:
Here’s how you use it: