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

gattacus

12/11/2018, 3:09 PM
What is the best/official way to write a custom MainCoroutineDispatcher? I wrote one for SWT but requires use of internal and experimental API
m

marstran

12/11/2018, 3:28 PM
If you're on the JVM, you could just create a new
Executor
with the
Executors
API (https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executors.html), and then convert it to a dispatcher with the
asCoroutineDispatcher
extension.
If you need it to be the main-dispatcher, I guess you need to somehow register it to a ServiceLoader, like the other main-dispatchers (https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/-main.html)
g

gattacus

12/11/2018, 3:31 PM
This doesn't give me a MainCoroutineDispatcher
I made it work but I wonder if there is a better way without internal/experimental api. the way via ServiceLoader smells
v

Vsevolod Tolstopyatov [JB]

12/11/2018, 3:44 PM
There are no official ways to write a custom dispatcher that will be available via
Dispatchers.Main
g

gattacus

12/11/2018, 3:50 PM
there should be. It is quite handy if you write code for different platforms. Android and desktop (SWT) in my case
v

Vsevolod Tolstopyatov [JB]

12/11/2018, 4:03 PM
Could you please file an issue on github with your use-case?
g

gattacus

12/11/2018, 4:04 PM
👍🏻
🙏 1
4 Views