Rxjava debounce 操作符

Debounce

1.官方定义

only emit an item from an Observable if a particular timespan has passed without it emitting another item

The Debounce operator filters out items emitted by the source Observable that are rapidly followed by another emitted item.

2.API

public final Observable<T> debounce(long timeout, TimeUnit unit);                       // 默认执行线程 Schedulers.computation()
public final Observable<T> debounce(long timeout, TimeUnit unit, Scheduler scheduler);

3.Android中使用场景

快速点击按钮,执行某个操作。

比如美团APP中的选择套餐:由左图的0份快速点击到右图的7份,然后根据选中份数计算总价。

4.代码实现一

// NumberPickerView.java
...
plusView.setOnClickListener(v - > {
      selectCount++;
      countTv.setText(selectCount + "");
      onChangeListener.onChange(dealId, selectCount);   // dealId为当前套餐的id
});


public interface OnChangeListener {
      onChange(int dealId, int selectCount);
}

 

// activity
...
numberPickerView.setOnChangeListener((dealId, selcetCount) -> {
      calculateDealPrice(dealId, selectCount);
});

private calculateDealPrice(int dealId, int selectCount) {
     ... // 计算价格
}

 

对于这种快速点击,我们其实需要的是对第7次进行计算,中间的一系列暂存态是没必要计算的,使用debounce来解决。

5.代码实现二:增加debounce操作

RxView.clicks(plusView)
             .map(aVoid -> {
                 selectCount++;
                 countTv.setText(selectCount + "");
                 return selectCount;
             }
             .debounce(400, TimeUnit.MILLISECONDS))
             .observeOn(AndroidSchedulers.mainThread())
             .subcribe(count -> onChangeListener.onChange(dealId, selectCount), Throwable::printStackTrace);  

 

缺点:

1.NumberPickerView依赖了 com.jakewharton.rxbinding:rxbinding:x.x.x

2.NumberPickerView中plusView被强制增加了400ms的debounce操作

5.代码实现三:将debounce操作移出NumberPickerView

// NumberPickerView.java
...
plusView.setOnClickListener(v - > {
      selectCount++;
      countTv.setText(selectCount + "");
      onChangeListener.onChange(dealId, selectCount);   // dealId为当前套餐的id
});

 

// activity
...
PublishSubject<SelectParams> subject = PublishSubject.create();
numberPickerView.setOnChangeListener((dealId, selectCount) -> {
      subject.onNext(new SelectParams(dealId, selectCount));
});

subject.debounce(400, TimeUnit.MILLISECONDS)
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(selectParams -> calculateDealPrice(selectParams.dealId, selectParams.selectCount), Throwable::printStackTrace);

class SelectParams {
   int dealId;
   int selectCount;
   SelectParams(int dealId, int selectCont) {
      this.dealId = dealId;
      this.selectCount = selectCount;
  }
}

private calculateDealPrice(int dealId, int selectCount) {
     ... // 计算价格
}

此时NumberPickerView不再依赖第三方库,适用性提高。

 

参考:http://reactivex.io/documentation/operators.html

转载于:https://www.cnblogs.com/ruyingxiangsui/p/6082777.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值