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

vineethraj49

11/20/2019, 4:12 PM
is there a way to schedule a block of code to run on a block completion when run with
withContext
? (I’m trying to time some functions)
along the same lines, would using
invokeOnCompletion
with async be a “good” idea
r

Rory Armstrong

11/20/2019, 4:15 PM
Not sure exactly what the use case is but maybe try
runBlocking
?
d

Dominaezzz

11/20/2019, 4:17 PM
What does "to run on a block completion" mean?
s

sikri

11/20/2019, 4:21 PM
Copy code
suspend fun someFun() = withContext(<http://Dispatcher.IO|Dispatcher.IO>) {
 …
}
or
coroutineScope
, please, use it, if you are able to call it from coroutine/suspend fun, as
runBlocking
is blocking, and
coroutineScope
is suspending
v

vineethraj49

11/20/2019, 4:23 PM
@Dominaezzz,
withContext
takes a parameter `block`; that block’s completion; think of it like
Copy code
try {
   block()
} finally {
   method()
}
d

Dominaezzz

11/20/2019, 4:24 PM
Oh, "block" -> "block's". Got it.
You can put
withContext
in try/finally, no?
v

vineethraj49

11/20/2019, 4:26 PM
hmm, withContextTimed with the same arguments, cool
will try that
4 Views