Colton Idle
08/14/2022, 5:08 PMval listFlow = snapshotFlow { appState.myList }
viewModelScope.launch {
listFlow.collectLatest {
Log...
but even when I clear the list and addAll to it, my flow doesn't get fired again (I don't see my log). Am I misunderstanding snapshotFlow, or should this work?Stylianos Gakis
08/14/2022, 7:55 PMmyList
?Colton Idle
08/14/2022, 7:56 PMStylianos Gakis
08/14/2022, 9:18 PMstateFlow
, we have to use it with T
where T
has to be backed by a MutableState
so that its observers get notified (this case the snapshotFlow has subscribed to see its changes and is observing that value), instead of passing a State<T>
in the stateFlow
lambda. That’s my understanding of it.
So this does not work for `snapshotFlow`:
val state: State<String> = produceState("") {
while(isActive) {
value = "one"
delay(1.seconds)
value = "two"
delay(1.seconds)
}
}
LaunchedEffect(Unit) {
snapshotFlow { state }.collectLatest {
println("state:$state")
}
}
But this does:
val state: String by produceState("") {
while(isActive) {
value = "one"
delay(1.seconds)
value = "two"
delay(1.seconds)
}
}
LaunchedEffect(Unit) {
snapshotFlow { state }.collectLatest {
println("state:$state")
}
}
In your case you got the type SnapshotStateList<T>
but you want to use snapshotFlow
on your <T>
instead.
This does not work
val state: SnapshotStateList<String> = mutableStateListOf("One", "Two")
LaunchedEffect(Unit) {
while (isActive) {
delay(1.seconds)
state.add("one")
delay(1.seconds)
state.add("two")
delay(1.seconds)
state.clear()
}
}
LaunchedEffect(Unit) {
snapshotFlow { state }.collectLatest {
println("state:$state")
}
}
But this does
This does not work
val state: SnapshotStateList<String> = mutableStateListOf("One", "Two")
LaunchedEffect(Unit) {
while (isActive) {
delay(1.seconds)
state.add("one")
delay(1.seconds)
state.add("two")
delay(1.seconds)
state.clear()
}
}
LaunchedEffect(Unit) {
snapshotFlow { state.toList() }.collectLatest {
println("state:$state")
}
}
Albert Chang
08/15/2022, 4:15 AMsnapshotFlow
.Stylianos Gakis
08/15/2022, 7:26 AM.toList()
😅
So for your original post do instead
val listFlow = snapshotFlow { appState.myList.toList() }
viewModelScope.launch {
listFlow.collectLatest {
Log...
Colton Idle
08/15/2022, 1:34 PM.toList()
Oh man. so much time wasted debugging this yesterday. lmaoooStylianos Gakis
08/15/2022, 1:44 PMSnapshotStateList
class to see if something that can extract the values exists and there wasn’t anything in there aside from get(index)
.
But my brain completely forgot to make the connection that SnapshotStateList
is in fact a MutableList
which is a List
which is a Collection
which is a Iterable
which you can call the public fun <T> Iterable<T>.toList(): List<T>
function on it.
Felt like I was back in the CS class on this one, learning Java inheritance and interfaces 😅Colton Idle
08/15/2022, 2:41 PMStylianos Gakis
08/15/2022, 2:43 PMSnapshotStateList
and since you’re doing it inside the snapshowFlow
it also subscribes to any changes it may get.
Could be wrong, but I wouldn’t even think about it.Chrimaeon
08/15/2022, 2:45 PMtoList()
will create a new list with the same content.Stylianos Gakis
08/15/2022, 3:03 PMChrimaeon
08/15/2022, 3:08 PMchannelFlow
builder that you can use to emit your “state”.Zach Klippenstein (he/him) [MOD]
08/15/2022, 4:04 PMsnapshotFlow
as the full restart scope. Eg you could do something like this:
viewModelScope.launch {
snapshotFlow {
appState.myList.forEach // or w/e
// put all your list processing here
// return type doesn't matter
}.collect()
}
Colton Idle
08/15/2022, 4:44 PM