https://kotlinlang.org logo
#coroutines
Title
# coroutines
d

Dev.Jang

06/30/2020, 7:42 AM
Hi guys. I have a question regarding this.
Copy code
private val searchWord = MutableLiveData<String>()

var searchResult = searchWord.switchMap { query ->
    liveData(<http://Dispatchers.IO|Dispatchers.IO>) {
        if (!query.isNullOrBlank()) {
            // I want to skip 'searchWord' input for 500 millisecond
            // And I want to get last response
            emit(userRepository.getApartment(query))
        }
    }
}
f

flosch

06/30/2020, 8:39 AM
In your case I would ditch LiveData and use
Flow.debounce()
. But with livedata it would be something like
Copy code
searchWord.asFlow()
    .debounce(500)
    .flatMapLatest { query ->  
        userRepository.getApartment(query)   
    }
    .flowOn(<http://Dispatchers.IO|Dispatchers.IO>)
d

Dev.Jang

06/30/2020, 9:13 AM
thank for the answer. it was very helpful :)
3 Views