Hey, I am trying to implement a search functionali...
# coroutines
r
Hey, I am trying to implement a search functionality that will hit the server endpoint. I am using
SharedFlow
,
collectLatest
and
debounce
so the endpoint won't be hit every time there is a text change. However,
collectLatest
is being called many times. why? and how can I fix it? full code
Copy code
searchText
                .debounce(1000)
                .distinctUntilChanged { prev, next -> prev != next }
                .filter { it.isNotEmpty() }
                .sample(500)
                .collectLatest {
                    Log.d("Remon", "search collect - value: $it")
                }
d
Are you sure that
distinctUntilChanged
has valid check? This lambda should check for elements equality and your current implementation does the opposite.
r
I am not sure
I changed it so
prev == next
... same result..
d
I think this should be enough
Copy code
searchText
                .debounce(1000)
                .distinctUntilChanged()
                .filter { it.isNotEmpty() }
                .collectLatest {
                    Log.d("Remon", "search collect - value: $it")
                }
r
still not working.. the collect block is called ~5 times
d
I checked this code and for me it seems to work. Can you give more context? How this flow is used? What is your expected behaviour?
r
I found the issue, for me I was calling the whole block of code many times
thank you so much for your help
d
No problem K
🔥 1
❤️ 1