Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FMDatabaseQueue freeze mainThread #817

Open
ordinaryman09 opened this issue Mar 25, 2021 · 2 comments
Open

FMDatabaseQueue freeze mainThread #817

ordinaryman09 opened this issue Mar 25, 2021 · 2 comments

Comments

@ordinaryman09
Copy link

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!

@ccgus
Copy link
Owner

ccgus commented Mar 25, 2021

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.

@robertmryan
Copy link
Collaborator

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants