1

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.

2 Answers 2

1

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();
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.