Hán Trung Kiên
1 min readSep 15, 2019

--

val liveData = liveData(context = viewModelScope.coroutineContext + Dispatchers.IO) {
...
}

The action of using “viewModelScope.coroutineContext” is unnessary because this “liveData” is controlled by lifecycleOwner (from Activity/Fragment) so we don’t care about it. In addtion this “liveData” is CoroutineLiveData which has scope by

CoroutineScope(Dispatchers.Main + context + supervisorJob)(Line 189 - CoroutineLiveData.kt - in this class consists more examples to use liveData block)

So we only need define context for repo’s api:

class MenuRepository @Inject constructor(
private val menuApi: MenuApi
) {
suspend fun getMenu(): List<Coffee> = withContext(Dispatchers.IO) {
return menuApi.getMenu()
}
}

--

--