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

rook

08/08/2019, 9:44 PM
I’m trying to make a
suspend
function execute its contents on a specific dispatcher. So far:``` suspend fun getFoo(): Foo? = coroutineScope { withContext(Dispatchers.IO) { retrieveFoo() } }``` Is this the best way to go about it?
o

octylFractal

08/08/2019, 9:46 PM
I don't think there's a need to open a new
coroutineScope
, just doing
withContext
should work
5
r

rook

08/08/2019, 9:47 PM
Ah, perfect, thanks!
g

gildor

08/09/2019, 2:51 AM
Also this is enough:
Copy code
<http://Dispatchers.IO|Dispatchers.IO> { retrieve Foo }
There is invoke operator for Dispatcher
👍 2
🔥 1
5
o

octylFractal

08/09/2019, 2:52 AM
Oh, that's pretty fancy.
e

Eric Martori

08/09/2019, 12:56 PM
@gildor is you example equivalent to withContext? Does invoking the Dispatcher propagate the coroutineContext?
g

gildor

08/09/2019, 12:59 PM
Yes, this is just a shortcut for withContext(SomeDispatcher)
e

Eric Martori

08/09/2019, 1:04 PM
I am trying it right now but can't find the
invoke
operator for
CoroutineDispatcher
in what version is this available? Or is it not part of the core?
g

gildor

08/09/2019, 1:05 PM
Autocomplete works not so well with invoke operator, I should create an issue about this, but invoke is a part of the core https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/invoke.html
e

Eric Martori

08/09/2019, 1:07 PM
I see, thank you
5 Views