``` private val uiJob = SupervisorJob() val uiScop...
# coroutines
s
Copy code
private 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.
g
How
getJourneys
is implemented? If your UI is blocked, than getJourneys is blocking function
s
with suspend function
retrofit call
g
Suspend function can be still blocking if implementation is not correct
s
yes i think so
g
does it use standard retrofit suspend function support?
or it’s your own implementation?
s
Copy code
@GET("/api/me/journeys")
	suspend fun getJourneys(@Header(TOKEN) token: String): List<Journey>
g
also:
Copy code
private val uiJob = SupervisorJob()
val uiScope = CoroutineScope(Dispatchers.Main + uiJob)
Can be replaced with:
val uiScope = MainScope()
Are you sure that you don’t have any nasty call adapters that may cause blocking? because it looks fine, nothing should be block. Also, could you check does it work if you replace
getJourneys()
with
delay(5000)
?
s
the problem of change MainScope to BackgroundScope is that i get an error assigning result of retrofit to mutablelivedata
for modifying ui thread in background thread
g
yes
I understand
as I said, ecveryhing looks completely fine
s
ok...
g
the problem of change MainScope to BackgroundScope
I do not suggesting this. Also
MainScope()
is just shortcut, it uses main dispatcher
s
the block that i am talking about is with viewpager when i change with gesture the view
the ui is blocked
I will try MainScope()
Thanks Andrey
g
Nono, It’s not a solution
I just said that you can write the same shorter
s
ah XD ok
g
also you can just get main scope from lifecycle/lifecycleObserver.coroutineScope
About your original problem, try replace
journeysService.getJourneys(token)
with placeholder like
delay(5000)
and check, will your UI is still blocking or not
s
ok
I will test it this evening Andrey, i cant now. Thanks for all.