0

I'm still new toRxJava and I am a bit confused on some code I have, where I don't have direct access to Observables, but pass an Observer as a parameter of some method to execute an Observable.

I would like to combine two Observers in some way but can't figure out how to do it with Observers.

Here I want to "combine" the mGetPotatoes and mGetBurger to show the potatoes and burger only when both do onNext (would be zip() for Observables I guess).

Show me some code

    mGetPotatoes.execute( new DisposableObserver<List<Potatoes>>() {
        @Override
        public void onNext(List<Potatoes> potatoes) {
            getMvpView().showPotatoes(mPotatoesMapper.mapPotatoesToViewModels(potatoes));
        }

        @Override
        public void onComplete() {

        }

        @Override
        public void onError(Throwable e) {
            getMvpView().hideProgress();
            getMvpView().showErrorMessage(e.getMessage());
        }
    });
    
    
    mGetBurger.execute( new DisposableObserver<Burger>() {
        @Override
        public void onNext(Burger burger) {
            getMvpView().showBurger(mBurgerMapper.mapBurgerToViewModel(burger));
        }

        @Override
        public void onComplete() {

        }

        @Override
        public void onError(Throwable e) {
            getMvpView().hideProgress();
            getMvpView().showErrorMessage(e.getMessage());
        }
    });

Edit

This question is a subset of this one where the template code for the mGetPotatoes and mGetBurger (that are UseCases) is.

Community
  • 1
  • 1
w00ly
  • 455
  • 1
  • 5
  • 18
  • you need to rethink your design to make the observables available. – ESala Jan 07 '18 at 11:01
  • That would be the easy solution, but I can't, it's part of a framework I don't have access to (Clean Architecture). I'm exploring different kind of possibilities, and this is one of them. – w00ly Jan 07 '18 at 11:06

2 Answers2

0

I would sugegst you to change your architecture so theObservable is returned and you can manipulate it (change the scheduler, perform some actions, etc)

In case that option is not feasible you may get a workaround with Subjects. A Subject is an Observer which is also an Observable.

For your case:

  1. Create a couple of PublishSubject
  2. Combine both (with the zip) operator and subscribe to the result
  3. Pass both subjects to your original use cases
Alberto S.
  • 7,409
  • 6
  • 27
  • 46
  • I can't change the architecure, and the Observable is added to a compositeDisposable (I edited my subject to add a link to the code of the template for the Burgers and Potatoes). Could you provide pseudo-code regarding the Subject? I only want to"synchronize" the operations so that I can show burgers and potatoes at the same time, if possible. – w00ly Jan 07 '18 at 16:03
  • I added the steps you have to make. Try to implement it by yourself with that help – Alberto S. Jan 07 '18 at 17:36
0

Not sure about what framework this is, but you can turn back the execute calls into Observables by wrapping them:

Observable<List<Potatoes>> obs = new Observable<List<Potatoes>>() {
    @Override public void subscribeActual(final Observer<? super List<Potatoes>> s) {
        mGetPotatoes.execute(new DisposableObserver<List<Potatoes>>() {

            @Override
            public void onStart() {
                s.onSubscribe(this);
            }

            @Override
            public void onNext(List<Potatoes> potatoes) {
                s.onNext(potatoes);
            }

            @Override
            public void onComplete() {
                s.onComplete();
            }

            @Override
            public void onError(Throwable e) {
                s.onError(e);
            }
        } 
    }
};
akarnokd
  • 69,132
  • 14
  • 157
  • 192