I am trying to migrate from RxJava1 to RxJava2. I replace all parts of the code where I previously had an Observable<Void> to Compleatable . However, I ran into one problem with the order of streaming calls. When I dealt with Observables earlier and used maps and flat maps, the code worked "as expected." However, the andthen() operator works slightly differently. Here is 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 magazines at the end:
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 (after that, the name of the andthen() operator assumes that the code will be called "after" Completable completes). I used to create an Observable<Void> using the Async.toAsync() operator, and the method now called getSingle was in the flatMap stream - it worked as I expected, so Log 4 would appear before 3. Now I tried changing the way Creating Compleatable - for example, using fromAction or fromCallable , but it behaves the same. I also could not find another operator to replace andthen() . To emphasize - the method must be Filled, because it does not have any thing that means a full return - it changes the application settings and other parameters (and is used in the same way as it basically works โas expectedโ), and these changes needed later in the stream. I also tried to wrap the getSingle() method in some way to create a Single and move the if statement inside the create block, but I don't know how to use the getSingleA / B () methods inside. And I need to use them, because they have their own complexity, and it makes no sense to duplicate the code. Anyone have an idea how to change this in RxJava2 so that it behaves the same? There are several places where I rely on the job to be completed to complete before moving forward with the stream (for example, update the session token, update db, settings, etc. - no problems in RxJava1 using flatMap).
source share