https://kotlinlang.org logo
#getting-started
Title
# getting-started
d

Daniele Segato

12/21/2020, 3:10 PM
I'm trying to use
MutableStateFlow
for the first time, but it is not doing what I expect
Copy code
private val state = MutableStateFlow<MyState>(INITIAL_STATE)

suspend fun refresh(): MyState {
  val newState = service.refresState()
  this.state.value = newState
  return newState
}

fun observeState(): Flow<MyState> {
  return state
    .transform { s ->
        if (s !== INITIAL_STATE) {
            emit(s)
        } else {
            refresh() // expecting this to automatically send a new state to the flow
        }
    }
}
I was expecing the
refresh()
to cause a new state to be emitted, but it didn't. Can someone guide me in the right direction? thanks.
j

Joost Klitsie

12/21/2020, 3:31 PM
is it an actual new object? StateFlow will only emit if it has a new (non-equal) object
If my memory serves me right
d

Daniele Segato

12/21/2020, 3:32 PM
oh, I didn't know it had a comparison in it, let me try changing it a bit
ah! it was clearly stated in the doc, I missed it, thanks
📃 1