ccgus / fmdb

A Cocoa / Objective-C wrapper around SQLite

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

FMDatabaseQueue freeze mainThread

ordinaryman09 opened this issue · comments

Hello,

I am running into some issue with FMDatabaseQueue for large inserts

Insertion 1 - Slow

self.dbQueue.inTransaction { (db, rollback) in
//can be lots of insertion. this runs fine by itself.
}

The problem is it takes a while, there could be another process running that reads/write into the DB
Insertion/Write 2 - Quick

self.dbQueue.inTransaction { (db, rollback) in 
//another insert/write
}

Insertion/Write 3 - Quick

self.dbQueue.inTransaction { (db, rollback) in 
//another insert/write
}

When checking the Insertion 1 threads, I don't see it blocking main thread.
I thought the dbQueue is queuing all the inTransaction or inDatabase automatically. Is this not the case? How are we suppose to handle these? I am using singleton for the FMDatabaseQueue. Thanks!

FMDatabaseQueue doesn't have to block the main queue. It just keeps multiple database operations from happening at the same time.

A backtrace of all the threads might be helpful in figuring out your problem. Also, spending some time in Instruments to figure out where it's being slow could help as well.

@ordinaryman09 - If you call inTransaction from the main queue, and that FMDatabaseQueue happens to be busy, you will end up blocking the main queue. Bottom line, inTransaction is synchronous (e.g. it adds the block of code via dispatch_sync). We are used to seeing code that takes a block/closure and just assume it is asynchronous, but in this case, it is synchronous.

While we should probably provide asynchronous renditions of inTransaction and inDatabase (e.g. #47), in the short term, you can dispatch these inTransaction calls to your own custom GCD queue.