zak.taccardi
10/13/2019, 1:44 AMDispatchers.Main.immediate
on Android?Adam Powell
10/13/2019, 1:54 AMDispatchers.Main
instead 🙂zak.taccardi
10/13/2019, 1:55 AMmarstran
10/13/2019, 9:34 AMPaul Woitaschek
10/13/2019, 9:39 AMIf you're asking whether you should or shouldn't use it in a particular situation, use the regularThat implies that there are situations whereinsteadDispatchers.Main
immediate
is not save. Why do you think so and when? @Adam PowelloctylFractal
10/13/2019, 7:07 PMimmediate
is not safe have to do with this text:
Immediate dispatcher is safe from stack overflows and in case of nested invocations forms event-loop similar to Dispatchers.Unconfined. The event loop is an advanced topic and its implications can be found in Dispatchers.Unconfined documentation.Looking in the Unconfined documentation, it seems to affect strict ordering of nested unconfined dispatches, which
immediate
would also be affected by. So, you should avoid nesting it, or just use the non-immediate version instead.zak.taccardi
10/13/2019, 7:09 PMoctylFractal
10/13/2019, 7:10 PMwithContext
callszak.taccardi
10/13/2019, 7:10 PMMain
, everything is posted to the main thread, while Main.immediate
may execute things immediately when there was work dispatched to the end of the Main
event loop?octylFractal
10/13/2019, 7:12 PMDispatchers.Unconfined
with the immediate one and the docs imply the same outcome is possibleAdam Powell
10/14/2019, 1:25 AMAdam Powell
10/14/2019, 1:26 AMwithContext
block. I wouldn't recommend it for things like flow collection or anything where you're going to suspend several times there unless you really know what you're doing.Adam Powell
10/14/2019, 1:27 AMAdam Powell
10/14/2019, 1:28 AMDispatchers.Main
uses an "async handler" under the hood (or an equivalent) - https://developer.android.com/reference/androidx/core/os/HandlerCompat#createAsync(android.os.Looper,%20android.os.Handler.Callback)Adam Powell
10/14/2019, 1:29 AMAdam Powell
10/14/2019, 1:29 AMAdam Powell
10/14/2019, 1:36 AMDispatchers.Main.immediate
include some circumstances like when you really don't want the ability for anything else on the main thread to happen before you resume, such as a lifecycle state changingAdam Powell
10/14/2019, 1:37 AMsavedInstanceState
Adam Powell
10/14/2019, 1:49 AMDispatchers.Main
won't be a problem. The way it interacts with the main thread looper is about as efficient of an event loop trampoline as you can get and it preserves expectations that a lot of code is written around. Some folks I know that do a lot of work with RxJava in production compared this style against a .immediate
-style scheduler they had written and didn't see an appreciable difference, but a lot of weird bugs in their code went away.