Sergio Crespo Toubes
08/27/2019, 6:24 AMprivate val uiJob = SupervisorJob()
val uiScope = CoroutineScope(Dispatchers.Main + uiJob)
val journeys = MutableLiveData<List<Journey>>()
fun loadJourneys() = uiScope.launch {
val token = preferences.getProfileToken() ?: return@launch
try {
journeys.value = journeysService.getJourneys(token)
}catch (e: Exception){
showGenericError.value = Unit
}
}
Hello i am trying coroutines with mvvm pattern. What i am doing bad with this? It´s working but the ui is blocked while is getting journeys. How can i fix this? Thanks.gildor
08/27/2019, 6:43 AMgetJourneys
is implemented? If your UI is blocked, than getJourneys is blocking functionSergio Crespo Toubes
08/27/2019, 6:44 AMgildor
08/27/2019, 6:44 AMSergio Crespo Toubes
08/27/2019, 6:44 AMgildor
08/27/2019, 6:44 AMSergio Crespo Toubes
08/27/2019, 6:45 AM@GET("/api/me/journeys")
suspend fun getJourneys(@Header(TOKEN) token: String): List<Journey>
gildor
08/27/2019, 6:46 AMprivate val uiJob = SupervisorJob()
val uiScope = CoroutineScope(Dispatchers.Main + uiJob)
Can be replaced with:
val uiScope = MainScope()
getJourneys()
with delay(5000)
?Sergio Crespo Toubes
08/27/2019, 6:47 AMgildor
08/27/2019, 6:48 AMSergio Crespo Toubes
08/27/2019, 6:49 AMgildor
08/27/2019, 6:49 AMthe problem of change MainScope to BackgroundScopeI do not suggesting this. Also
MainScope()
is just shortcut, it uses main dispatcherSergio Crespo Toubes
08/27/2019, 6:49 AMgildor
08/27/2019, 6:50 AMSergio Crespo Toubes
08/27/2019, 6:50 AMgildor
08/27/2019, 6:50 AMjourneysService.getJourneys(token)
with placeholder like delay(5000)
and check, will your UI is still blocking or notSergio Crespo Toubes
08/27/2019, 6:51 AM