Is there a reason to NOT use `MainDispatcher.immed...
# coroutines
m
Is there a reason to NOT use
MainDispatcher.immediate
over just regular
MainDispatcher
?
l
Yes, allowing other coroutines to run (cooperatively), or worse, tight loops.
👍 1
m
tight loops
you basically risk stack overflow here?
l
No, you risk blocking the main thread.
m
ok, but what specific to tight loops is the problem?
or just the general blocking of the main thread?
l
If you have a tight loop on a coroutine where the suspension points resume immediately, and you use the immediate main dispatcher, you get the main thread to block.
👍 1
e
In addition, it makes order of events in your code harder to reason about.
👍 1
p
And the opposite? When should or would one use .immediate?
e
There is some small performance benefit with
immediate
. I’m not Android developer to tell whether it even remotely important, but generally I would not trade a small performance improvement for a code that is harder to reason about.
p
But why is it even there then?
e
Some people think that this performance benefit is worth it and we (in Kotiln) in general are trying to provide tools for as large number of use-cases as possible. However, we pick defaults on the safe side, that is why you have to explicitly opt-in to
immediate
if you needed and understand all the problems around it.
p
And can you write in the javadoc what the implications are?
1