Currently, I am trying to optimize my app performance by improving the usage of different Dispatchers and contexts. One question I stumbled upon is that If I launch a suspend function inside a coroutine with a IO Dispatcher, will every other function be executed in the same dispatcher as well?
Example
fun doSomething() {
viewModelScope.launch(Dispatchers.IO) {
getUserData(viewModelScope)
}
}
fun getUserData(innerScope: CoroutineScope) {
workerList.startUserDataWorker()
observeUserData(innerScope) // suspend function, is this called inside the IO Dipatcher?
}
// Will this be called inside the IO Dispatcher?
private suspend fun observeUserData(innerScope: CoroutineScope) {
observerWorkerStateAndPassData(workerList.userDataWorkInfo, USER_DATA_OUTPUT_OPTION).collect { status ->
when(status) {
is Status.Loading -> {
_userDataState.postValue(Status.loading())
}
is Status.Success -> {
// Will getShippingAddressList() also be called on the IO Dispatcher?
_userDataState.postValue(Status.success(getShippingAddressList()))
}
is Status.Failure -> {
_userDataState.postValue(Status.failed(status.message.toString()))
}
}
}
}
// Getting Address from the local room cache. Is this called on the IO Dispatcher?
private suspend fun getShippingAddressList(): List<UserDeliveryAddress> {
val uncachedList = userAddressDao.getAllAddress(UserAddressCacheOrder.SHIPPING)
return userAddressCacheMapper.mapFromEntityList(uncachedList)
}