when calling a `suspend fun` exposed from a librar...
# coroutines
w
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
Retrofit and SQLDelight are safe
🙌 2
l
Wait, SQLDelight is exposing suspending functions?
w
i think i may have misspoke on that (unless something has changed in the past few months)
j
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
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
is Room not main-safe by default? trying to research a bit but haven’t found out yet
l
Room is always main-safe
w
i believe you - but is there any documentation showing this to show my co-workers?
l
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
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
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
perfect to share those with my co-workers and for me to read over. thanks!!