# coroutines

Nemanja Scepanovic

07/01/2020, 1:41 PM
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?


07/01/2020, 4:06 PM
Database operations should be done on IO dispatcher. You could use like this: flow.asLiveData(viewModelScope.coroutineContext)

Nemanja Scepanovic

07/01/2020, 4:19 PM
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 🤔


07/01/2020, 11:46 PM
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

Nemanja Scepanovic

07/02/2020, 10:32 AM
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 🙂