https://kotlinlang.org logo
#coroutines
Title
# coroutines
r

rook

04/13/2021, 4:19 PM
I’m noticing a little bit of inconsistent behavior with
invokeOnCompletion
. Given the following
Copy code
launch(Dispatchers.Default) {
  val theJob = async(<http://Dispatchers.IO|Dispatchers.IO>) {}

  // do some work

  theJob.invokeOnCompletion { println() }
}
If
do some work
takes longer than
theJob
to complete, the handler block runs on
Dispatchers.Default
, whereas if
theJob
takes longer than
do some work
, the handler block runs on
<http://Dispatchers.IO|Dispatchers.IO>
.
d

diesieben07

04/13/2021, 4:21 PM
From the docs (https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/invoke-on-completion.html):
Note: Implementation of CompletionHandler must be fast, non-blocking, and thread-safe. This handler can be invoked concurrently with the surrounding code. There is no guarantee on the execution context in which the handler is invoked.
💯 1
r

rook

04/13/2021, 4:22 PM
Thank you
2 Views