> Room will use different Dispatchers for trans...
# coroutines
s
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.
Does that mean if I use
withContext(<http://Dispatchers.IO|Dispatchers.IO>) { //db calls }
then likelihood is that Room merges this with whatever
Architecture Components IO executor
it uses internally? Or would
<http://Dispatchers.IO|Dispatchers.IO>
overwrite Room's default dispatcher. As far as I understand
Default
means
main
in Android.
g
It depends how you define your dbCalls
if you use Room correctly with coroutines, you really want to define all those room quaries as suspend functions: https://medium.com/androiddevelopers/room-coroutines-422b786dc4c5 And if you do so, this dispatcher.io wrapping is useless (and create unnecessary scope
s
I can run db calls without making them suspend functions. How do I decided if those should be made
suspend
functions or not?
g
I can run db calls without making them suspend functions
Of course you can, but my point that if you already use them with coroutines, you shouldn’t do that Without “suspend” those db calls compiled to standard blocking functions, which not safe to use from main thread (or even any other suspend functions)(), so it’s a lot better to make them suspend, as described in article above
s
Thanks Andrey. That was very helpful. Thank you for taking the time to reply.
👍 1