Pick the fix
Typing 'ca' then 'cat' sometimes shows results for 'ca'. Which change fixes it?
val results: Flow<List<Article>> = query
.debounce(300)
.distinctUntilChanged()
.flatMapMerge { q -> flow { emit(api.search(q)) } }Hint 1 of 1
What happens to the 'ca' request when 'cat' arrives?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A Replace flatMapMerge with flatMapLatest
Correct. A new query cancels the old request, so its result can't arrive later.
Why not the others?
- B Increase debounce to 1,000 ms. That makes the race rarer and the app slower; it doesn't remove it.
- C Add
.distinctUntilChanged()after the search. Different results aren't duplicates, so nothing changes. - D Collect on
Dispatchers.IO. Threading doesn't change which response arrives last.