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

Animesh Sahu

04/27/2020, 8:24 AM
How to implement Dispatchers.Main and bind the main thread into it?
unfortunately I don't think you can bind to
Dispatchers.Main
because it's internal, but this is close
a

Animesh Sahu

04/27/2020, 8:28 AM
Ohh nice. Thanks for the help 😊
@octylFractal Btw what are immediate dispatchers, never heard of em.
o

octylFractal

04/27/2020, 8:39 AM
it's part of the
Dispatcher.Main
setup, allowing you to dispatch on the same thread if you're already on the main thread. essentially this pseudocode:
if (mainThread) runnable() else dispatch(runnable)
a

Animesh Sahu

04/27/2020, 8:39 AM
ohh
o

octylFractal

04/27/2020, 8:40 AM
that's the effect if you do
scope.launch(mainDispatcher.immediate) { ... }
a

Animesh Sahu

04/27/2020, 8:42 AM
what if when launching without immediate when currently on main?
o

octylFractal

04/27/2020, 8:42 AM
it acts just like a normal dispatcher then
a

Animesh Sahu

04/27/2020, 8:44 AM
How about this approach? This uses the backing event-loop to do the same, https://stackoverflow.com/a/54195483/11377112
o

octylFractal

04/27/2020, 8:46 AM
it's okay, you can't get the same
immediate
benefit though
a

Animesh Sahu

04/27/2020, 8:51 AM
ohh, now i understood. It'd be better to use something like your implementation. Thanks again for your time 😉😊😁
4 Views