I am creating a coroutine using the script below:
fun bar(completion: () -> Unit) {
GlobalScope.launch(Dispatchers.IO) {
val lambda = {
withContext(Dispatchers.Main) { //Suspension functions can be called only within coroutine body
completion()
}
}
foo(lambda)
}
}
fun foo(lambda: () -> Unit) {
//...do something heavy
lambda()
}
But I am getting the error Suspension functions can be called only within coroutine body
when I call withContext(Dispatchers.Main)
because the lambda changes the context. I cannot change foo(lambda: () -> Unit)
to foo(lambda: suspend () -> Unit)
because it's from an external library.
Any idea of what I can do to call withContext(Dispatchers.Main)
inside the lambda created inside the launch
context?
- Kotlin version: 1.3.41
- Coroutines: 1.3.0-RC
非常感谢.