RxJava:掉落物品? - 背压

RxJava: Drop items? - Backpressure

我使用 RxJava 来观察几个按钮的点击。

这些订阅将在一个对象上调用不同的函数,这需要几毫秒的时间。这些功能是同步的。

问题是,当按下太多按钮时,我会遇到背压异常。对我有用的是放弃几个输入(最好是旧的)。 RxJava 可以吗?

这就是 onBackPressureDrop() 的用途:

Instructs an Observable that is emitting items faster than its observer can consume them to discard, rather than emit, those items that its observer is not prepared to observe.

对于 RxJava 3,您可以使用新的 Flowable 概念:

    observable.toFlowable(BackpressureStrategy.LATEST)

您可以选择不同的策略:

    /**
     * OnNext events are written without any buffering or dropping.
     * Downstream has to deal with any overflow.
     * <p>Useful when one applies one of the custom-parameter onBackpressureXXX operators.
     */
    MISSING,
    /**
     * Signals a MissingBackpressureException in case the downstream can't keep up.
     */
    ERROR,
    /**
     * Buffers <em>all</em> onNext values until the downstream consumes it.
     */
    BUFFER,
    /**
     * Drops the most recent onNext value if the downstream can't keep up.
     */
    DROP,
    /**
     * Keeps only the latest onNext value, overwriting any previous value if the
     * downstream can't keep up.
     */
    LATEST