Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6825095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:58:54+00:00 2026-05-26T21:58:54+00:00

My Sqlite Database is accessed from various threads in my code due to which

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:58:54+00:00Added an answer on May 26, 2026 at 9:58 pm

    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:

    // The queue needs to be a global variable, or globally accessible in some way.
    dispatch_queue_t dbQueue;
    // Make your sqlite3 connection global too.
    sqlite3 *dbConnection;
    
    // In your application delegate:
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // usual initialization ...
        ...
        int rc = sqlite3_open(dbFilename, &dbConnection);
        if (rc != SQLITE_OK)
            handleDbError(rc, dbConnection);
        dbQueue = dispatch_queue_create("dbQueue", DISPATCH_QUEUE_SERIAL);
        ...
    }
    

    Here’s how you use it:

    dispatch_queue_async(dbQueue, ^{
        // This block runs off the main thread, and does not run simultaneously
        // with any other blocks submitted to `dbQueue`.
        NSString *result = executeDatabaseQuery();
    
        dispatch_queue_async(dispatch_get_main_queue(), ^{
            // This block runs on the main thread.
            [(MyAppDelegate *)[UIApplication delegate] presentResult:result];
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble querying my SQLite database using Android. I keep getting either an
My model objects are lazy-loaded from an SQLite database. Don't ask why, but the
I am designing an application that fetches data from a sqlite database on the
I am using the SQLite database and have the following persistent class (simplified): public
I have an sqlite database full of huge number of URLs and it's taking
I have a SQLite database that contains a huge set of log messages. I
I created a SQLite database on Android device. The program can read/write to database
I have an sqlite database, and I would like to keep it read-only without
I have an SQLite database, eventually will be a MySQL database and I'm using
I am using an sqlite database for a producer-consumer queue. One or more producers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.