One Off the Slack: Effects of Effects

len asked:

Is there any difference between LaunchedEffect and DisposableEffect when you don’t actually need any cleanup? I’ve seen in the official samples some DisposableEffects with an empty onDispose {} , and that makes me think LaunchedEffect should be a better option, but I’m in doubt 🤔

Google’s Adam Powell responded:

If you need to publish changes from composition to external objects you can use SideEffect

There are some trivial differences in overhead and one not so trivial difference in behavior between DisposableEffect and LaunchedEffect; LaunchedEffect has slightly more overhead from the coroutines machinery but if you’re noticing a difference from that then you’re using this pattern way too much. LaunchedEffect also won’t start running until the next frame due to coroutine dispatcher behavior, but DisposableEffect will run its setup code after composition apply but before measure/layout/draw happens for the current frame. Sometimes this can affect correctness or jank one way or the other but only for fairly rare cases.

If you only ever use effects to publish results of composition and not to try to affect the current composition, you’ll never notice a correctness issue from this

If an effect can synchronously alter inputs of both composition and layout/drawing then you can see a shear where for that frame, layout/draw will see new data but the composition will still reflect old data

len seemed grateful for the clarification:

Oh, I see! I knew LaunchedEffect was delayed by a frame, but I didn’t know DisposableEffect isn’t delayed…

My initial thinking was that they’re the same but one allows to dispose of something if the key changes or it leaves the composition

Read the original thread in the kotlinlang Slack workspace. Not a member? Join that Slack workspace here!