https://kotlinlang.org logo
#compose
Title
# compose
p

Pablichjenkov

10/11/2023, 6:00 PM
Is there a way to control the order of execution of SideEffect, LaunchEffect and or DisposableEffect. Or is it a bad practice having more than one of these in one Composable function.
s

Stylianos Gakis

10/11/2023, 6:05 PM
If one depends on another, why not have them be part of the same effect?
2
f

Francesc

10/11/2023, 6:52 PM
there are no guarantees in which order they will run. Furthermore, a
LaunchedEffect
runs on a non-immediate dispatcher, so it will be scheduled to run at a later time. You should not rely on their order. It's fine to have multiple of them, if they have different keys. Otherwise, group them by common keys.
a quick test shows that disposble effect runs first, followed by side effect, followed by launched effect. Within this order, they run in the order they are defined in the composition, but this is an implementation detail that you should not rely on.
z

Zach Klippenstein (he/him) [MOD]

10/11/2023, 7:46 PM
No, I’m pretty sure that’s part of the contract. Effects start in order and are disposed in reverse order. Otherwise it would be impossible to reliably define dependencies between effects. LaunchedEffects are coroutines though so they don’t get dispatched until after the choreographer frame is finished, even though the coroutine is technically launched at the same time as disposed effects.
👍 2
👍🏻 1
s

shikasd

10/11/2023, 7:59 PM
DisposableEffect
is backed by remember observer and it is dispatched first, then side effects, then
LaunchedEffect
is run on the next frame. Between effects, they are guaranteed to be run in declaration order and disposed in the opposite order (as Zach mentioned above)
Note that this order is a part of the contract and you can rely on it, and Compose UI does use it quite extensively.
2
f

Francesc

10/11/2023, 8:19 PM
DisposableEffect
is backed by remember observer and it is dispatched first, then side effects, then
LaunchedEffect
is run on the next frame.
this is what I saw, so it's not the order they are in the composition (only by groups). Is this documented somewhere?
p

Pablichjenkov

10/12/2023, 2:16 AM
Yeah I noticed the same Fracesc. Good to know fellows, I appreciate your help.
4 Views