Mark
07/22/2022, 2:54 PMFlow<T>.distinctUntilChanged(areEquivalent: (old: T, new: T) -> Boolean)
but emits the newest of the equivalent values instead of the oldest?Adam Powell
07/22/2022, 3:03 PMstojan
07/22/2022, 3:03 PMdebounce()
instead? (typically in combination with mapLatest
)
that way you ignore keystrokes for a certain time duration. That's the pattern I typically use for search with autocompleteAdam Powell
07/22/2022, 3:04 PMDominaezzz
07/22/2022, 4:11 PMdebounce
I just do mapLatest
with a delay in it before the actual fetching.Adam Powell
07/22/2022, 4:31 PMstojan
07/22/2022, 4:58 PMunique to flows if you're used to rxactually
mapLatest
is similar to switchMap/switchMapSingle
depending on plain on suspend functionjw
07/22/2022, 11:10 PMMark
07/23/2022, 1:02 AMdebounce
, however, it’s not enough because there is no value that can adequately distinguish between a slow typer (this is an east asian language-learning app where the learner could legitimately take many seconds to type a character) and someone who is looking up a word, seeing the results, and quickly moving on. Debounce works well when the user is typing fast or backspacing the search text to oblivion. So I need some best-effort logic that will discard search text that is clearly a stepping stone to what the user is actually aiming for, though this logic is beyond the scope here. Regarding the part about never emitting the final value, my initial thought was that’s okay because as soon as the user clears the search box, then it’s emitted, and that leaving the app (onPause
) could act as a trigger to flush the current search text. However, that latter part sounds messy, and makes me lean towards placing the discarding logic in the data source where it would potentially replace the latest history item instead of just adding.Chris Fillmore
07/23/2022, 8:04 PMdebounce
overload which accepts a lambda (T) -> Long
where you can set the debounce value dynamically based on the value emitted.
https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/debounce.htmlMark
07/31/2022, 3:26 AM