1

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;
            }
        });
Nouman Bhatti
  • 1,777
  • 4
  • 28
  • 55
  • use flatmapIterable and then flatmap and inside flatmap you can use a scheduler in case you use rxjava2 – Raghunandan Feb 11 '18 at 17:06
  • @Raghunandan any code? because i have read about flatmap but dont know how to actually use it in my code – Nouman Bhatti Feb 11 '18 at 17:10
  • something like this https://pastebin.com/F60KtMyc ? – Raghunandan Feb 11 '18 at 17:23
  • I don't see anything different between the first and second code snippet – nhoxbypass Feb 11 '18 at 17:29
  • @Raghunandan this is so confusing or i have very little knowledge of rxjava. Here is what i have got so flatMapIterable will return items one by one from database and pass it to flatmap and then i perform operation on each item there but where do i receive the final list? And flatmap is expecting ObservableSource to return but in example its filter object. – Nouman Bhatti Feb 11 '18 at 17:53
  • @NoumanBhatti when you do some operation in flatmap return Observable and then call toList(). I just gave you gist of how you can use it. – Raghunandan Feb 11 '18 at 17:55
  • I would recommend playing with the RxMarbles App.... – Appyx Feb 22 '18 at 14:41

0 Answers0