The rxJava debounce () statement does not work with Observable.range ()

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!

+5
source share
4 answers

debounce prevents downstream congestion by determining the delay period that must elapse between events in order to receive the last event, in other words, it will exit the last element after some quiet time. The range will go through its elements so quickly that it can be, so there will not be enough time between the elements, and only the latest will be released.

debounce is simply not the operator that your use case requires. The extension project for 2.x has a spanout statement for your use case.

+4
source

What you need is an operator on .delay (). See description here - http://reactivex.io/RxJava/javadoc/rx/Observable.html#delay-rx.functions.Func1-

 Observable .range(1, 10000) .delay(new Func1<Integer, Observable<Long>>() { @Override public Observable<Long> call(Integer integer) { return Observable.timer(1, TimeUnit.SECONDS ); } }); 

For good order, ".debounce ()" is absolutely excluded for this use case.

+2
source

use delay instead of debounce

 Observable.range(1, 10000) .delay(1000, TimeUnit.MILLISECONDS) .subscribe(...); 
+1
source
  Observable.range(1, 10000) .debounce(500, TimeUnit.MILLISECONDS) // Run on a background thread .subscribeOn(Schedulers.io()) // Be notified on the main thread .observeOn(AndroidSchedulers.mainThread()) .subscribe(....); 

Try this way.

+1
source

Source: https://habr.com/ru/post/1273952/


All Articles