Is it possible to implement something like next chaining using RxJava:
loginObservable() .then( (someData) -> { // returns another Observable<T> with some long operation return fetchUserDataObservable(someData); }).then( (userData) -> { // it should be called when fetching user data completed (with userData of type T) cacheUserData(userData); }).then( (userData) -> { // it should be called after all previous operations completed displayUserData() }).doOnError( (error) -> { //do something })
I found this library very interesting, but can't figure our how to chain requests where each other depends on previous.
subscribeOn is executed during the subscription process and affects both upper, and lower streams. It also can change the thread as many times as you write it. The one closest to the top of the chain is applied. observeOn affects only the lower streams, and is executed during emission.
RxJava, once the hottest framework in Android development, is dying. It's dying quietly, without drawing much attention to itself. RxJava's former fans and advocates moved on to new shiny things, so there is no one left to say a proper eulogy over this, once very popular, framework.
Map transforms the items emitted by an Observable by applying a function to each item. FlatMap transforms the items emitted by an Observable into Observables. So, the main difference between Map and FlatMap that FlatMap mapper returns an observable itself, so it is used to map over asynchronous operations.
This is what flatMapCompletable does: Maps each element of the upstream Observable into CompletableSources, subscribes to them and waits until the upstream and all CompletableSources complete.
Sure, RxJava supports .map
which does this. From the RxJava Wiki:
Basically, it'd be:
loginObservable() .switchMap( someData -> fetchUserDataObservable(someData) ) .map( userData -> cacheUserData(userData) ) .subscribe(new Subscriber<YourResult>() { @Override public void onCompleted() { // observable stream has ended - no more logins possible } @Override public void onError(Throwable e) { // do something } @Override public void onNext(YourType yourType) { displayUserData(); } });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With