-1

I'm observing click listener on 2 buttons to increase or decrease counter with the observer below

            observer = new Observer<Long>() {
                @Override
                public void onSubscribe(Disposable disposable) {
                    compositeDisposable.add(disposable);
                }

                @Override
                public void onNext(Long value) {
//                System.out.println(" mObserver onNext() int: " + value);
                    dataBinding.text1.setText("Observer onNext() int: " + value);

                }

                @Override
                public void onError(Throwable e) {
                    dataBinding.text1.setText("Observer onError() e: " + e.getMessage());

                }

                @Override
                public void onComplete() {
                    dataBinding.text1.setText("Observer onComplete()");
                }
            };
        }

And one of the observables i add to button is this

Observable<View> viewObservableMinus = Observable.create(emitter ->

            dataBinding.buttonMinus.setOnClickListener(view -> {

                System.out.println("Inside onClickListener");

                emitter.onNext(view);

                emitter.setCancellable(() ->{
                    System.out.println(" dataBinding.buttonMinus emitter.setCancellable()");
                    view.setOnClickListener(null);
                } );

            }));

    viewObservableMinus.map(view -> (long) --mCounter).subscribe(observer);

When i touch the button twice i get the messages below and click listener is set to null, why does it set to null? If don't use setCancellable it works fine by the way.

I: Inside onClickListener
I: Inside onClickListener
I:  dataBinding.buttonMinus emitter.setCancellable()
Thracian
  • 43,021
  • 16
  • 133
  • 222

1 Answers1

2

From documentation:

void setCancellable(@Nullable Cancellable c) Sets a Cancellable on this emitter; any previous Disposable or Cancellable will be disposed/cancelled.

Basically, when you call setCancellable() for the second time there is another Cancellable associated with the emitter and app will call dispose on the first Cancellable added before, then will add new Cancellable.

Edit: You would need to change your code to something like that to set Cancellable only once, not on every click on the button probably.

   Observable viewObservableMinus = Observable.create(emitter ->
            emitter.setCancellable(() ->{
                System.out.println(" dataBinding.buttonMinus emitter.setCancellable()");
                view.setOnClickListener(null);
            });    

            dataBinding.buttonMinus.setOnClickListener(view -> {
                System.out.println("Inside onClickListener");

                emitter.onNext(view);
            }));

    viewObservableMinus.map(view -> (long) --mCounter).subscribe(observer);

Reference: http://reactivex.io/RxJava/javadoc/io/reactivex/ObservableEmitter.html

MDikkii
  • 273
  • 1
  • 10
  • Interesting. Every answer on SO suggest it to use like i do. For instance [this](https://stackoverflow.com/a/43560538/5457853), and [this](https://stackoverflow.com/a/40721508/5457853) answers. I know and use RxBindings but i also trying to test it with the way i asked. How should it be done then with Observable? – Thracian Jul 25 '19 at 10:47
  • First answer shows call of the setCancellable in proper way [here](https://stackoverflow.com/a/43560538/5457853). Please read it carefully once again. – MDikkii Jul 25 '19 at 10:54