Sam Schilling
01/21/2020, 8:08 PMIllegalStateException
? I don’t want to have to wrap every launch call in runblocking whenever I want to do some background work, but is there another way around that?
// Fails
runBlocking {
GlobalScope.launch {
println(1)
GlobalScope.launch { // IllegalStateException: no event loop, use runBlocking
println(2)
}
}
}
// Succeeds
runBlocking {
GlobalScope.launch {
println(1)
runBlocking {
GlobalScope.launch {
println(2)
}
}
}
}
Kris Wong
01/21/2020, 8:09 PMKris Wong
01/21/2020, 8:09 PMSam Schilling
01/21/2020, 9:00 PMrunBlocking
call blocks the calling thread until its coroutines, i.e., the nested launches, are complete.basher
01/21/2020, 9:06 PMSam Schilling
01/21/2020, 9:06 PMbasher
01/21/2020, 9:08 PMrunBlocking {
GlobalScope.launch {
}
}
works since you're explicitly calling out to a scope that isn't the runBlocking
. without understanding that, it's hard to elaborate my initial answer 🙂basher
01/21/2020, 9:09 PM