IOS uses Fmdb multithreading to access databases and databaseislocked issues

Source: Internet
Author: User

Principle: File database SQLite allows multiple processes/threads to read at the same time, but only one thread is allowed to write at the same time. In the write operation, the database file is locked, at this time any other read/write operations are blocked, if the block more than 5 seconds (the default is 5 seconds, can recompile SQLite can modify the timeout time), reported "database is locked" error.

Therefore, in the operation of SQLite, you should close the connection immediately, after opening the connection, to minimize the very time-consuming operation.

When multiple threads access the database at the same time, the problem of database locking is reported, and the error message is:

Unknown error finalizing or resetting statement (5:database is locked)

Finally, through Fmdatabasequeue solves this problem, this article summarizes:

Fmdatabase cannot use one instance of multithreading

multi-threaded access to the database, cannot use the same An instance of Fmdatabase. Otherwise, an exception will occur. If a thread is allowed to use a separate fmdatabase instance, there is also the possibility that the database is locked problem occurs. This is due to the competition caused by multi-thread to SQLite

Fmdatabasequeue solves this problem by creating a queue and then executing the block order of the queued queues, which avoids multiple threads accessing the database at the same time

Solution sharing the same Fmdatabasequeue instance

Create a DBHelper class

Use Fmdatabasequeue after that, the management DB

Originally using the Fmdatabase class, you need to manually invoke the open and close methods of the DB

But with Fmdatabasequeue, you don't need to call open, because the view code finds that the queue is open. As to whether to close, I am not sure, because the official sample code did not call close. In practical applications, I did not call, as if there were no problems.

So, using queue, it is not necessary to open and close the db yourself. But if you use fmresultset,rs you need to close, otherwise you will be reported warning:

if ([db hasopenresultsets]) {

NSLog (@ "Warning:there is at least one open result set around after performing [Fmdatabasequeue indatabase:]");

In order not to see warning, I have called in the block [Rs Close]

Refreshing the database file path

Specific to our application, there is a special problem to consider. Because our app can switch accounts, the DB file of the account is independent. So when the user logs back in, the helper queue needs to be refreshed

If you do not, because the helper is a singleton, then after switching accounts, User B will be accessing user A's database. Refreshed calls, typically placed after login, are available before entering the main page

IOS uses Fmdb multithreading to access databases and databaseislocked issues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.