hi guys, given ```viewModelScope.launch(<http://Di...
# coroutines
j
hi guys, given
Copy code
viewModelScope.launch(<http://Dispatchers.IO|Dispatchers.IO>) {
    block()
    foo1()
}
If block’s body contains
Copy code
viewModelScope.launch(Dispatchers.Main) {
            foo2()
        }
foo1() is never executed. can somebody explain me why?
z
is your block() suspending?
o
is that the entirety of
block()
?
f
Are you cancelling the scope somewhere?
j
is your block() suspending?
yes sir (hi, i know from twitter, what’s up)
is that the entirety of 
block()
?
no, that code is executed during flow.collect{} method
Are you cancelling the scope somewhere?
no i am not
o
hmm, I think i'd have to see more code --
flow.collect {}
sounds like it might be the issue, but if it only happens when you add that
launch
, I can't make sense of it
j
thanks for the private help, @octylFractal. problem was block() throwing
Copy code
kotlinx.coroutines.JobCancellationException: Job was cancelled; job=SupervisorJobImpl{Cancelling}@e1ab056
I will investigate why
d
why not use withContext()
o
it has different semantics to
launch
?
j
I understood the issue: block() shows a new fragment -> Viewmodel#onCleared is called -> viewModelScope’s job is canceled. when foo1() is called, the job was canceled.
any elegant solution to this? 🙂
o
assuming you just need to cleanup resources,
finally
is probably a good option here