https://kotlinlang.org logo
#flow
Title
# flow
t

Tiago Nunes

04/08/2021, 4:35 PM
Hi everyone, is there a way to get the the previously emmited value of a flow in the onEach function, or something like this?
Copy code
private val refreshJob = filterFlow
    .onEach { prev, new ->
        if(prev.x != new.x) {
            updateX()
        }
    }
    .launchIn(viewModelScope)
I know I can separate the filterFlow in multiple flows, which is probably the better way of solving my use case, but I'm still curious
e

ephemient

04/08/2021, 4:44 PM
Copy code
filterFlow.fold(null) { prev: T?, new ->
    if (prev == null || prev.x != new.x) {
        updateX()
    }
    new
}
👍 1
or
Copy code
filterFlow.reduce { prev, new ->
    if (prev.x != new.x) {
        updateX()
    }
    new
}
if you don't need that initial unpaired value
👍 2