Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJava2 order of sequence called with compleatable andThen operator

I am trying to migrate from RxJava1 to RxJava2. I am replacing all code parts where I previously had Observable<Void> to Compleatable. However I ran into one problem with order of stream calls. When I previously was dealing with Observables and using maps and flatMaps the code worked 'as expected'. However the andthen() operator seems to work a little bit differently. Here is a sample code to simplify the problem itself.

public Single<String> getString() {
    Log.d("Starting flow..")
    return getCompletable().andThen(getSingle());
}

public Completable getCompletable() {
    Log.d("calling getCompletable");
    return Completable.create(e -> {
                Log.d("doing actuall completable work");
                e.onComplete();
            }
    );
}

public Single<String> getSingle() {
    Log.d("calling getSingle");
    if(conditionBasedOnActualCompletableWork) {
        return getSingleA();
    }else{
        return getSingleB();
    }
}

What I see in the logs in the end is :

  1-> Log.d("Starting flow..")
  2-> Log.d("calling getCompletable");
  3-> Log.d("calling getSingle");
  4-> Log.d("doing actuall completable work");

And as you can probably figure out I would expect line 4 to be called before line 3 (afterwards the name of andthen() operator suggest that the code would be called 'after' Completable finishes it's job). Previously I was creating the Observable<Void> using the Async.toAsync() operator and the method which is now called getSingle was in flatMap stream - it worked like I expected it to, so Log 4 would appear before 3. Now I tried changing the way the Compleatable is created - like using fromAction or fromCallable but it behaves the same. I also couldn't find any other operator to replace andthen(). To underline - the method must be a Completable since it doesn't have any thing meaning full to return - it changes the app preferences and other settings (and is used like that globally mostly working 'as expected') and those changes are needed later in the stream. I also tried to wrap getSingle() method to somehow create a Single and move the if statement inside the create block but I don't know how to use getSingleA/B() methods inside there. And I need to use them as they have their complexity of their own and it doesn't make sense to duplicate the code. Any one have any idea how to modify this in RxJava2 so it behaves the same? There are multiple places where I rely on a Compleatable job to finish before moving forward with the stream (like refreshing session token, updating db, preferences etc. - no problem in RxJava1 using flatMap).

like image 657
Adam J Avatar asked Apr 01 '17 13:04

Adam J


1 Answers

You can use defer:

getCompletable().andThen(Single.defer(() -> getSingle()))

That way, you don't execute the contents of getSingle() immediately but only when the Completablecompletes and andThen switches to the Single.

like image 119
akarnokd Avatar answered Oct 16 '22 11:10

akarnokd