Hi guys! I was wondering, is it OK to pass a Corou...
# coroutines
a
Hi guys! I was wondering, is it OK to pass a CoroutineDispatcher converted to Executor to methods that accept it? For example, I have a
LocationClient.requestLocationUpdates
method call wrapped in
callbackFlow
that accepts an Executor, and I'm passing
Dispatchers.IO.asExecutor()
to it. Not sure if it makes any difference in terms of performance. Maybe, it's better to construct a separate Executor for this, and close it in
awaitClose
? It's not clear if there is any overhead with either approach. I didn't have much luck benchmarking the callbacks, too Thread in Slack Conversation
w
I do this when calling async Java APIs that want an executor from coroutines code. Also sometimes the inverse; I'll use
Executor.asCoroutineDispatcher()
from a Java API that gives me an executor to do things on. I can't say that it's the best possible approach, but it hasn't hurt me yet. My theory is that it's better to reduce the number of thread pools as long as the intent of the dispatcher matches the intent of the executor. What I mean by that is don't pass
Dispatchers.Default.asExecutor()
or
Dispatchers.Main.asExecutor()
to something that is going to do IO on the executor.