AFAIK, the rxJava debounce() statement is used to delay the allocation of events. When I apply it in the search box, it works fine:
RxTextView.textChangeEvents(editText) .debounce(1000, TimeUnit.MILLISECONDS) //Only emit after 1 sec .subscribe(new Observer<TextViewTextChangeEvent>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(TextViewTextChangeEvent event) { //Get more information about text change event Log.e(TAG, "Before: " + event.before() + ", start: " + event.start() + ", count: " + event.count()); } @Override public void onError(Throwable e) { } @Override public void onComplete() { Log.e(TAG, "textChangeEvents: onComplete"); } });
But when I apply it with Observable.range() as follows:
Observable.range(1, 10000) .debounce(1000, TimeUnit.MILLISECONDS) .subscribe(new Observer<Long>() { @Override public void onSubscribe(@NonNull Disposable d) { } @Override public void onNext(@NonNull Long integer) { } @Override public void onError(@NonNull Throwable e) { } @Override public void onComplete() { } });
the radiation continues to approach onNext() very quickly (about 1000 emissions / s) and continuously, although I applied the debounce(1000, TimeUnit.MILISECONDS) operator debounce(1000, TimeUnit.MILISECONDS) .
I would like to expect : when I use debounce() , after a delay of 1000 milliseconds, only 1 number will be issued (it can skip numbers when there is a delay between two outliers). The radiation values will move downstream one by one, as an example of the search box above.
I'm new to rx, please help me achieve this and explain why? I don’t know why I SHOULD use another operator, but debounce() , because the idea is the same!
source share