Hi guys, quick question, is it considered safe to ...
# coroutines
n
Hi guys, quick question, is it considered safe to use flow.asLiveData() method without setting a different dispatcher as a parameter (method is using the Dispatcher.Main.immediate by default), in cases where we are just observing data changes from the database? Is it considered normal to have data emitting on this Dispatcher.Main.immediate dispatcher?
r
Database operations should be done on IO dispatcher. You could use like this: flow.asLiveData(viewModelScope.coroutineContext)
n
Hmm, this code you provided will force Dispatcher.Main.immediate (implementation of viewModelScope) which is kinda the same as doing just flow.asLiveData(). But the interesting thing is this part of the code inside CoroutinesRoom class (screenshot below), which shows that ‘observing’ db changes is done on specific db dispatcher, while only the emitting part will be done on caller’s context. Since this isn’t the db operation per-se but rather just the observing/emitting, i’m not quite sure if context should be something specific like IO 🤔
g
You need custom dispatcher in this case only if it required for your code Nothing wrong with emitting data on Main thread by itself, it just depends what you do with this value after
n
Cool, thank you so much, i suppose if I'm doing some sort of computation or mapping with the values then it is appropriate to have perhaps Default dispatcher or smth like that 🙂