Hi everyone, is there a way to get the the previou...
# android
t
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
z
scan
and return the new item
👍 1
i
Also, you might try #coroutines for future questions around Flows
👍 1
t
@Zach Klippenstein (he/him) [MOD] I only care about the last 2 emmited values, how I implement this with scan?
z
Have you looked at the scan function? It gives you the previous and new values
a
Judging by the contents of the
forEach
, is
map { it.x }.distinctUntilChanged().onEach { updateX() }
not sufficient?
🙌 1
t
Hm I think all I need is distinctUntilChanged, it can take a
(*old*: T, *new*: T) -> *Boolean*
lambda
Thanks @Andy Byrnes 🎉
👍 1