Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlite3 - iOS - database is locked

Tags:

sqlite

xcode

ios

I am developing an app for ipad and i am using sqlite sentences (select, update, insert, delete).

I open (sqlite3_open) the database at the beginning and close (sqlite3_close) at the end of each sentence. But sometimes i´ve got the "database is locked" message.

I don´t know what can i do to solve this.

Thanks and sorry for this little information.

like image 790
javiazo Avatar asked Oct 22 '25 03:10

javiazo


1 Answers

If I'm not mistaken , the problem with sqllite is that you can only access it once at a time. If you have multiple threads, you can run in this situation. Example:

Run method1 (which accesses the database) on thread t1. Run method2 (which accesses the database) on thread t2 after x seconds.

If method1 is not finished in those x seconds , both methods will access it at the same time. And , as I said , I know that sqllite does not support this.

You should try to flag the usage of your database and if you want to access it but it is in use , try again after x seconds. Like this:

- (void) generalMethodThatUsesDatabses
{
    if(databaseIsUsed)
    {
         [self performSelector:@selector(generalMethodThatUsesDatabses) withObject:nil afterDelay:5];
          return;
    }

    databaseIsUsed = TRUE;   //global bool variable


    //your code here

    databaseIsUsed = FALSE;

}

Hope this helps. Cheers!

like image 181
George Avatar answered Oct 24 '25 18:10

George



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!