thefrugaldev
thefrugaldev

Reputation: 1629

Multiple API calls using RxJava and Android

I have a list of objects which should be passed to an API sequentially and need a final callback when all calls are completed. The code structure looks like

//Singleton class
public class RestAPIManager {

    public Completable doSomething(A a) {
        //Make API call here and return Completeable
    }
}

//Class from which the above API call is made
public class FragmentA extends Fragment {
    List<A> aList = new ArrayList<A>;

    //This method should return completable after all calls are made
    private Completable callDoSomething(List<A> data) {
        // The code that I tried
        Observable.just(data)
            .flatMap(new <List<A>, Observable<?>>() {
                @Override
                public Observable<?> call(List<A> dataList) {
                    //WHAT SHOULD BE RETURNED FROM HERE
                    for (A a : dataList) {
                        RestAPIManager.getInstance().doSomething(a)    
                    }
                }
            })
            .doOnCompleted(new Action0() {
                @Override
                public void call() {

                }
            })
            .doOnError(new Action1<Throwable>() {
                @Override
                public void call(Throwable throwable) {

                }
            })
            .toCompletable(); 
    }
}

I have just started on using RxJava so am really not sure of how to go ahead with this.

Upvotes: 1

Views: 1024

Answers (2)

ROBOHORSE
ROBOHORSE

Reputation: 11

For android use something like this

public void doIt(List<String> data) {
    final Subscription subscription = proceed(data)
            .subscribe(new Subscriber<List<String>>() {
                @Override
                public void onCompleted() {
                    //final callback here
                    System.out.println("on completed");
                }

                @Override
                public void onError(Throwable e) {
                    //invokes on error
                    System.out.println("on error");
                }

                @Override
                public void onNext(List<String> strings) {
                    //invokes on items emits
                    strings.forEach(System.out::print);
                }
            });
}

private Observable<List<String>> proceed(List<String> data) {
    return Observable.from(data)
            .map(RestApiManager.getInstance()::doSomething)
            .toList();
}

and unsubscribe from request execution by subscription.unsubscribe(); when onStop() invokes to avoid leaks. And don't forget about schedulers

Upvotes: 0

Maksim Ostrovidov
Maksim Ostrovidov

Reputation: 11058

You can use from operator to flatten the list, then concatMap to process each item sequentially:

Observable.from(data)
    .concatMap(new Func1<A, Observable<?>>() {
        @Override
        public Observable<?> call(A a) {
            return RestAPIManager.getInstance().doSomething(a);
        }
    })
    .toCompletable();

Upvotes: 1

Related Questions