接收到 RxJava Debounce 方法调用

RxJava Debounce method call received

我的意思是,我想为方法调用做一个简单的限制。 我有这个方法:

public void called(int number){
   doSomething(number);
}

此方法调用过于频繁,我想根据收到的号码创建一个可观察对象,并 debounce 调用 doSomething(number)

这可能吗?

当然可以,像这样-

PublishSubject<Integer> subject = PublishSubject.create();

subject
  .debounce(1, TimeUnit.SECONDS)
  .subscribe(new Action1<Integer>() {
    public void call(Integer number) {
      doSomething(number);
    }
  });

public void called(int number){
  subject.onNext(number);   
}
private void doSomeWork() {
 getObservable().debounce(500, TimeUnit.MILLISECONDS)
            // Run on a background thread
            .subscribeOn(Schedulers.io())
            // Be notified on the main thread
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(getObserver());
}

 private Observable<Integer> getObservable() {
    return Observable.create(new ObservableOnSubscribe<Integer>() {
        @Override
        public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
            doSomething(number);
        }
    });
}

这可能对您有所帮助