Why Single.concat(.., ..).toObservable() does not return only one observable to observe ?
I am working on search feature, on Android Room database, from different sources with same result type.
I want to show results in order from first source then second source.
@Query("SELECT id, title, from table1 where title LIKE :title || '%'")
fun getItemsFirstTable(title: String): Single<List<MinimumResult>>
@Query("SELECT id, title, from table2 where title LIKE :title || '%'")
fun getItemsSecondTable(title: String): Single<List<MinimumResult>>
method to concat Single and returns an Observable in view model
public Observable<List<SearchItem>> concatSearchedItems(String s){
return Single.concat(UseCaseKt.getItemsFirstTable(s), UseCaseKt.getItemsSecondTable(s)).toObservable()
}
bellow the TextWatcher
Observable.create(new ObservableOnSubscribe<Object>() {
@Override
public void subscribe(ObservableEmitter<Object> emitter) throws Exception {
mEditTextSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
emitter.onNext(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
})
.subscribeOn(Schedulers.io())
.doOnNext( l -> Log.d(TAG, "onCreateView: "))
.debounce(500, TimeUnit.MILLISECONDS)
.distinctUntilChanged()
.switchMap(new Function<Object, ObservableSource<List<SearchItem>>>() {
@Override
public ObservableSource<List<SearchItem>> apply(Object o) throws Exception {
return searchViewModel.concatSearchedItems(o.toString());
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<List<SearchItem>>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(List<SearchItem> searchItems) {
searchViewModel.searchList.setValue(searchItems);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
After debugging I found that onNext is called twice despite I already concat Single results.
Single.concat
does not concat data from emits of Singles, but concat Singles it self.
So you need to use zip :
public Observable<List<SearchItem>> concatSearchedItems(String s){
return Singles.zip(UseCaseKt.getItemsFirstTable(s), UseCaseKt.getItemsSecondTable(s))
{ tab1list, tab2list -> listOf<SearchItem>().plus(tab1list).plus(tab2list) }
.toObservable()
}