https://kotlinlang.org logo
#coroutines
Title
# coroutines
s

Sudhir Singh Khanger

06/08/2021, 4:37 AM
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

gildor

06/08/2021, 5:20 AM
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

Sudhir Singh Khanger

06/08/2021, 11:50 AM
I can run db calls without making them suspend functions. How do I decided if those should be made
suspend
functions or not?
g

gildor

06/09/2021, 3:21 AM
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

Sudhir Singh Khanger

06/09/2021, 4:43 AM
Thanks Andrey. That was very helpful. Thank you for taking the time to reply.
👍 1
3 Views