Reputation: 4586
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
Upvotes: 16
Views: 12025
Reputation: 87430
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());
Upvotes: 53