Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJava: How to run two sequential calls : second depends on first

I need to create an observable where 2 blocks of retrofit based calls are run sequentially. I know I can just run the second retrofit call inside one Observer call but it will be pretty messy. I have my Observable code in a separate class from the caller and it returns an Observable. I'd like to pass the result of the first call to the second then when the second call is done pass back an Observable to the calling class. (I'm using Java 7 and not 8)

public class GetFollowing {

 public Observable< ArrayList<Media> >  init() {
    return Observable.create(
                    new Observable.OnSubscribe< ArrayList<Media> >() {
                        @Override
                        public void call(Subscriber<? super  ArrayList<Media> > subscriber) {

...

I also need to pass back to the calling class a different type than I pass to teh second retrofit call. I been reading about map flatMap and concat but I can't seem to figure out how to structure them for my use here.

UPDATE

I came up with this, not sure if its the most elegant or if it will work at all...but if it does work is there any way to pass the result of first observable to second? Also how would I handle an issue if first observable fails?

Observable< ArrayList<Media> > test;

         Observable.concat(

                Observable.create(
                        new Observable.OnSubscribe< ArrayList<User> >() {
                            @Override
                            public void call(Subscriber<? super  ArrayList<User> > subscriber) {

                            }
                        }
                ),
                 test = Observable.create(
                        new Observable.OnSubscribe< ArrayList<Media> >() {
                            @Override
                            public void call(Subscriber<? super  ArrayList<Media> > subscriber) {

                            }
                        }
                )

        );

        return test; 
like image 319
Mike6679 Avatar asked Mar 10 '23 23:03

Mike6679


1 Answers

If the the requirement can be rephrased as below:

  1. You have two methods to be executed and both return Observables.
  2. The items emitted on first method's Observable needs to be fed into the second method as and when they occur.
  3. The output of second method is an Observable which is based on some computation on items of first Observable.

The readily available flatMap feature in RxJava is the solution for you. Below is a simple implementation to assist you.

public static void main(String[] args) {
    Observable<Integer> o1 = Observable.just(1, 2);
    Observable<String> result = o1.flatMap(result1 -> Observable.just("Value is: "+result1));
    result.subscribe(finalResult -> System.out.println("Final result: "+finalResult));
}

Output:

Final result: Value is: 1
Final result: Value is: 2

On the other side, if second method does not return an Observable, but performs some operation on the emitted item, you can implement the same using map.

like image 104
Pavan Kumar Avatar answered Apr 25 '23 11:04

Pavan Kumar