I am trying to combine LiveData
with RxBinding
library, I want to use RxSearchView
and use switchMap
in order to subscribe to the latest observable. I am not sure if my implementation is good. I'll show you my code, please could you tell me if there is another way to achieve this?
disposable = RxSearchView.queryTextChanges(searchView)
.debounce(300, TimeUnit.MILLISECONDS)
.switchMap(charSequence -> {
detailViewModel.loadSearchTask(charSequence);
return Observable.just(charSequence);
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe();
compositeDisposable.add(disposable);
public void loadSearchTask(CharSequence s) {
taskInteractor
.search(s.toString())
.doOnSubscribe(disposable -> rxSearchLiveData.addDisposable(disposable))
.subscribe(
charSequence -> rxSearchLiveData.setValue(Resource.success(charSequence)),
throwable -> error.setValue(Resource.error(throwable.getMessage(), throwable)));
}
Edit: Should I use a Flowable
instead?