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

Matej Drobnič

05/25/2019, 7:57 AM
Is there a reason to NOT use
MainDispatcher.immediate
over just regular
MainDispatcher
?
l

louiscad

05/25/2019, 8:45 AM
Yes, allowing other coroutines to run (cooperatively), or worse, tight loops.
👍 1
m

Matej Drobnič

05/25/2019, 8:52 AM
tight loops
you basically risk stack overflow here?
l

louiscad

05/25/2019, 8:56 AM
No, you risk blocking the main thread.
m

Matej Drobnič

05/25/2019, 9:09 AM
ok, but what specific to tight loops is the problem?
or just the general blocking of the main thread?
l

louiscad

05/25/2019, 9:11 AM
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

elizarov

05/25/2019, 11:07 AM
In addition, it makes order of events in your code harder to reason about.
👍 1
p

Paul Woitaschek

05/26/2019, 12:26 PM
And the opposite? When should or would one use .immediate?
e

elizarov

05/26/2019, 12:28 PM
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

Paul Woitaschek

05/26/2019, 12:29 PM
But why is it even there then?
e

elizarov

05/26/2019, 12:30 PM
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

Paul Woitaschek

05/26/2019, 12:53 PM
And can you write in the javadoc what the implications are?
1
2 Views