krzysztof
06/09/2022, 9:03 AMmutableStateListOf
changes in effects?
Say, I have Parent
which has state remember { mutableStateListOf<Int>() }
which can be changed by one of its children. Parent
also has an effect (let’s say DisposableEffect
) reading changes to the list, like DisposableEffect(list){/* ... */}
. But that effect won’t be called more than once, because list
won’t change instance (SnapshotStateList
), even if items are added.ste
06/09/2022, 11:16 AMonListChanged: () -> Unit
to let the children notify the parent when it modifies the list;
2- Use list.size
as key instead of size
, however I'm not sure how it would react when swapping two elements;
3- Depends on your use caseSaurabhS
06/09/2022, 1:00 PMsnapshotFlow()
. It should emit when the list is updated.Zach Klippenstein (he/him) [MOD]
06/09/2022, 3:17 PMsnapshotFlow
is for. Or any of the lower-level apis for snapshot observation.krzysztof
06/09/2022, 3:20 PMval list = remember { mutableStateListOf<Int>() }
LaunchedEffect(Unit) {
snapshotFlow { list }.collect {
Log.i("XXX", "snapshot updated!")
}
}
and then calling
list.add(2)
should print snapshot updated!
, correct?Zach Klippenstein (he/him) [MOD]
06/09/2022, 3:29 PMkrzysztof
06/09/2022, 3:39 PMsnapshotFlow { list }
, to be updated whenever the list is modified. But it’s not the case here. If I do snapshotFlow { list.size }
then it’s working as expected. Like you said, list
has the same identity (due to remember)Zach Klippenstein (he/him) [MOD]
06/09/2022, 3:43 PMkrzysztof
06/09/2022, 3:52 PMLaunchedEffect(Unit) {
snapshotFlow { Log.i("XXX", "list size: ${list.size}") }.collect {}
}
mutableListStateOf
down to children (as List<T>) and in there, I have LaunchEffect(list)
call, which is not re-executed when list is updated.I’ll have to find different way thenZach Klippenstein (he/him) [MOD]
06/09/2022, 3:59 PM@Composable fun Foo(list: List<T>) {
LaunchedEffect(list) {
snapshotFlow { list.… }.collect()
}
}
krzysztof
06/10/2022, 8:17 AMDisposableEffect
that should listen for changes in list
, and that effect does not own coroutine scope, so it’s bit tricker(FYI There’s a collect override that doesn’t take any params that’s good for this use case)Yup, found it, has to be exported as it’s extension 👌
Zach Klippenstein (he/him) [MOD]
06/10/2022, 4:20 PMkrzysztof
06/10/2022, 7:31 PMlist
changes 😄Zach Klippenstein (he/him) [MOD]
06/10/2022, 9:23 PMkrzysztof
06/12/2022, 6:42 PMonDisposed
called, so my guess for LaunchedEffect would be doing a cleanup when its coroutine is cancelled?Zach Klippenstein (he/him) [MOD]
06/13/2022, 3:31 PM