RxAndroid textview events called automatically before text change events

George Thomas picture George Thomas · Dec 15, 2015 · Viewed 10.5k times · Source

I used rxandroid for debounce operation on an edittext search

I used

private void setUpText() {
        _mSubscription = RxTextView.textChangeEvents(searchStation)//
                .debounce(500, TimeUnit.MILLISECONDS)// default Scheduler is Computation
                .observeOn(AndroidSchedulers.mainThread())//
                .subscribe(getOps().getdata());
    }

and observer as

public Observer<TextViewTextChangeEvent> getdata()
    {

        return new Observer<TextViewTextChangeEvent>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                 e.printStackTrace();
            }

            @Override
            public void onNext(TextViewTextChangeEvent onTextChangeEvent) {
//                stationsugession(onTextChangeEvent.text().toString());

                //here i called link to get the data from the server
            }
        };
    }

My problem is the link is called even before any edittext changes occurs. And its not calling the textchange events. Am i missing something What am i doing wrong here. I am new to rxandroid and rxjava.

I used

  compile 'io.reactivex:rxandroid:1.1.0'
    compile 'io.reactivex:rxjava:1.1.0'
    compile 'com.jakewharton.rxbinding:rxbinding:0.2.0'

EDIT:

It now works now, i was getting a null pointer in my logic for getting list.. when i used the onError method and put a stacktrace i found the problem.

And if you want to skip the initial call then we should call .skip(1) to your subscription object. [thanks to Daniel Lew ]

The above code is working perfectly now

Answer

Dan Lew picture Dan Lew · Dec 15, 2015

RxTextView.textChanges() emits the current text value of the TextView before emitting any changes. See the documentation.

If you want to only debounce the changes, then you should add skip(1), which will ignore the initial emission:

_mSubscription = RxTextView.textChangeEvents(searchStation)
    .skip(1)
    .debounce(500, TimeUnit.MILLISECONDS)
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(getOps().getdata());