Thomas
10/22/2024, 2:00 AMLaunchedEffect
side effect I usually see two patterns,
Pattern 1:
LaunchedEffect(state.value){
doSomething()
}
Pattern 2:
LaunchedEffect(state){
snapshotFlow{ state.value }.collect{ doSomething() }
}
1. When do we prefer using one over the other?
2. Are there difference in the two patterns from the point of view of the doSomething
function?
3. Would it be different if doSomething
takes in state.value
as argument for pattern 1, and it
in pattern 2?m.c.shin
10/22/2024, 2:02 AMval
, so Pattern 1 is right.Thomas
10/22/2024, 2:03 AMState
into a Flow
... Is this correct?Thomas
10/22/2024, 2:05 AMval
State
object as the key for LaunchedEffect
I am curious about this too, whats wrong with just passing Unit
when the state variable is val
?Fergus Hewson
10/22/2024, 2:06 AMThomas
10/22/2024, 2:07 AMLaunchedEffect
is followed immediately by a snapshotFlow
, such that the collect
block is fired every time the state changesThomas
10/22/2024, 2:08 AMsnapshotFlow
is collected on "enter composition", and is alive until the parent composable exits compositionFergus Hewson
10/22/2024, 2:09 AMm.c.shin
10/22/2024, 2:09 AMThomas
10/22/2024, 2:10 AMdoSomething
function to be something like sending analytics information 🤔Thomas
10/22/2024, 2:12 AMThomas
10/22/2024, 2:13 AMdoSomething
funciton invocations 👀
More specifically I wonder if there are "gotchas" to pattern 2m.c.shin
10/22/2024, 2:14 AMm.c.shin
10/22/2024, 2:15 AMm.c.shin
10/22/2024, 2:15 AMThomas
10/22/2024, 2:21 AMdoSomething
task handled basically the same by the CoroutineDispatcher
?
If I am not mistaken, the CoroutineDispatcher
of both the LaunchedEffect
block and the snapshotFlow
block is exactly the same as the one used by the Recomposer
, therefore they should behave the same? 🤔m.c.shin
10/22/2024, 2:25 AMm.c.shin
10/22/2024, 2:26 AMThomas
10/22/2024, 2:56 AMstate.value
, therefore the parent composable was never observing changes to the state
. Because of this the parent don't necessarily have to recompose when state.value
changesBen Trengrove [G]
10/22/2024, 4:13 AMBen Trengrove [G]
10/22/2024, 4:15 AMstate
rather than Unit
because you need to relaunch if the actual state
holder class changes (not the value in it), or else you would still be observing the old oneThomas
10/22/2024, 5:26 AMremember
retainment lifetime to see the issues 🐵
Thanks again for all the answers!Viktor Nyblom
10/22/2024, 6:36 AM