George Theocharis
08/04/2020, 10:48 AMGiorgos Neokleous
08/04/2020, 10:52 AMcollect
the same way as a Flow
. If you are actively collecting/observing it should emit all the values. The value
will always be the current value.George Theocharis
08/04/2020, 10:53 AMwasyl
08/04/2020, 10:58 AMStateFlow
should work the same wayGeorge Theocharis
08/04/2020, 11:01 AMGiorgos Neokleous
08/04/2020, 11:13 AMonEach { print(it) }
can you see all the updates? đ¤George Theocharis
08/04/2020, 11:17 AMstreetsofboston
08/04/2020, 12:01 PMGeorge Theocharis
08/04/2020, 12:50 PMwasyl
08/04/2020, 12:58 PMStateFlow
only emits if the state has changed so youâd also not get every value emittedstreetsofboston
08/04/2020, 1:05 PMpostValue
method is called, instead of its value
setter.
Try running your test (both emissions and collectors) on the Main Immediate dispatcher and call yield
before collecting the next value.
https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-main-coroutine-dispatcher/immediate.htmlGiorgos Neokleous
08/04/2020, 1:13 PMval scope = TestCoroutineScope()
@Test
fun testStateFlow() = scope.runBlockingTest {
val stateFlow = MutableStateFlow(0)
val values = mutableListOf<Int>()
val job = launch {
stateFlow
.onEach { values.add(it) }
.collect()
}
launch {
while (stateFlow.value != 5) {
stateFlow.value = stateFlow.value + 1
}
}
scope.advanceUntilIdle()
Truth.assertThat(values).containsExactly(0, 1, 2, 3, 4, 5).inOrder()
job.cancel()
scope.cleanupTestCoroutines()
}
This works for me fine đ¤ Isn't this what you are describing?streetsofboston
08/04/2020, 1:36 PMGeorge Theocharis
08/04/2020, 2:45 PM