https://kotlinlang.org logo
Title
w

William Reed

07/12/2021, 2:18 PM
when calling a
suspend fun
exposed from a library like
Retrofit
or
Room
or
SqlDelight
- is there a need to change the dispatcher to
IO
or since they are code generating a
suspend fun
is it safe to assume that its ‘main-safe’?
j

jw

07/12/2021, 2:19 PM
Retrofit and SQLDelight are safe
🙌 2
l

louiscad

07/12/2021, 2:23 PM
Wait, SQLDelight is exposing suspending functions?
w

William Reed

07/12/2021, 2:23 PM
i think i may have misspoke on that (unless something has changed in the past few months)
j

jw

07/12/2021, 2:30 PM
it exposes Flow
mutators are synchronous so that you can batch in a transaction and run everything with a single dispatch (and without the incredibly complicated hacks that Room has to do)
l

louiscad

07/12/2021, 2:32 PM
I knew about Flow, but that's not suspending functions. You need to make sure you execute the resulting
Query
objects on
<http://Dispatchers.IO|Dispatchers.IO>
to be main-safe. (That is not an argument for Room)
w

William Reed

07/12/2021, 2:37 PM
is Room not main-safe by default? trying to research a bit but haven’t found out yet
l

louiscad

07/12/2021, 2:38 PM
Room is always main-safe
w

William Reed

07/12/2021, 2:39 PM
i believe you - but is there any documentation showing this to show my co-workers?
l

louiscad

07/12/2021, 2:39 PM
SQLDelight, for now, unfortunately, the answer depends on what you do. I wish that, at least in development mode, it'd throw by default if you try run on an app's main thread that is not in JS.
well thanks guys for the help
g

gildor

07/13/2021, 1:48 AM
I would say that general rule, that any suspend function must be “main-safe” aka non blocking. If some library exposes suspend function which is blocking, it’s 100% bug of the library and should be reported
s

Sudhir Singh Khanger

07/13/2021, 5:47 AM
FYI Room has its own Architecture Components IO dispatcher which is specialized for Room operations. You don't need to switch dispatchers.
Room will use different Dispatchers for transactions and queries. These are derived from the executors you provide when building your Database or by default will use the Architecture Components IO executor. This is the same executor that would be used by LiveData to do background work.
https://medium.com/androiddevelopers/room-coroutines-422b786dc4c5
w

William Reed

07/13/2021, 1:48 PM
perfect to share those with my co-workers and for me to read over. thanks!!