How to perform queries in background thread?

729 Views Asked by At

I'm currently looking at having a KMM application backed by SQLdelight for all domain-related operations.

SQLdelight seems to provide really nice interfaces, however it seems like all the write calls (insert/update/delete) are implemented using blocking calls, so I'm worried that it would hurt the responsiveness of the app by blocking the main thread a lot.

Is there a recommended way to perform such operations without blocking the main thread?

The app would have to work on iOS as well, so I can't afford freezing too much.

1

There are 1 best solutions below

0
On

A bit late to answer but it might be useful for others:

You should use wiwthContext(Dispatchers.Default) assuming you are using the native-mt version of coroutine libraries. That allow you to ensure insert/update/delete are not executed on the main thread.

You also have the possibility of using sqldelight coroutine-extensions library to return a flow from your queries to observe changes in your database.