Possible Duplicate RxJava performing operation on a list and returning an observable
I am very new to rxJava. What I want to achieve is to get data from Database as ArrayList
and perform some operation on each item of ArrayList
. I want to do all that in a single rxjava call/code. Following is my code to get list of items from Database that is working fine.
getCompositeDisposable().add(getAllFilters()
.subscribeOn(getSchedulerProvider().io())
.observeOn(getSchedulerProvider().ui())
.subscribe(new Consumer<List<Filter>>() {
@Override
public void accept(List<Filter> filters) throws Exception {
}
}));
public Observable<List<Filter>> getAllFilters() {
return Observable.fromCallable(new Callable<List<Filter>>() {
@Override
public List<Filter> call() throws Exception {
return mAppDatabase.filterDao().getFilters();
}
});
}
What I want is to perform some operation on each item of list in background thread and generate final list. To do that I have to make another rxjava call like below:
getCompositeDisposable().add(applySomeOperation()
.subscribeOn(getSchedulerProvider().io())
.observeOn(getSchedulerProvider().ui())
.subscribe(new Consumer<List<Filter>>() {
@Override
public void accept(List<Filter> filters) throws Exception {
}
}));
But I want to do both of these tasks in a single call. Get list of items from database and then perform a background task on each item of list and then finally return a List. I have read about map
, flatMap
and concatMap
but don't know how to use it in this context. Currently I have been able to write this code but don't know how it works
getDataManager().getAllFilters().concatMap(new Function<List<Filter>, ObservableSource<?>>() {
@Override
public ObservableSource<?> apply(List<Filter> filters) throws Exception {
return null;
}
});