https://kotlinlang.org logo
#coroutines
Title
# coroutines
i

Ivan Pavlov

03/10/2021, 2:45 PM
Hi. I have something like
Copy code
val sf1 = MutableStateFlow(listOf(1))
val sf2 = MutableStateFlow(listOf(-1))

sf1.value = listOf(1, 2)
sf2.value = listOf(-1, -2)
sf1.value = listOf(1, 2, 3)
I combine them like
Copy code
sf1.combine(sf2) { a, b -> a + b }
    .stateIn(scope, SharingStarted.Eagerly, initialValue = listOf())
    .onEach { println(it) }
    .launchIn(scope)
Then I don't have [1, -1] list in combined flow. I changed initialValue to sf1.value + sf2.value and now it works but I think that I could miss something which simplifies this. Is there a better way?
m

myanmarking

03/11/2021, 10:20 AM
by the time you use combine, the first value for sf1 get’s lost because only the last value is ‘cached’. It is my understanding
4 Views