Search code examples
fmdb

FMDatabaseQueue was called reentrantly error


I have a wrapper around my sqlite database which creates objects and handles all database queries. It contains only SELECT queries. It is nicely factored into reusable pieces and is working well.

Recently I added a query that is running slowly (multiple joins) and so I wanted to run it on a background queue.

I created an FMDatabaseQueue and wrapped all my calls to db.executeQuery inside queue.inDatabase { db in ... }.

But when FMDB detects that I am calling inDatabase from within another inDatabase call, it bombs out:

inDatabase: was called reentrantly on the same queue, which would lead to a deadlock.

What is the correct way to handle this? It seems that I need to restructure my code to ensure that there is never a nested call to the db, but I don't understand why, since sqlite has no problem with simultaneous reads.


Solution

  • FMDatabaseQueue can't detect if you're going to do an update or start a transaction or if you're only doing reads. So it always says "nope" to reentrant calls.

    If you are only doing reads, you might instead use a pool of connections.