1

I use RxJava to observe clicks on several buttons.

These subscription will invoke different functions on an object which takes several milliseconds. These functions are synchronized.

The problem is that when too many buttons are pressed I get a backpressure exception. What would work for me would be to drop several inputs (preferrably the oldes ones). Is that possible with RxJava?

Paul Woitaschek
  • 6,717
  • 5
  • 33
  • 52

2 Answers2

5

This is what onBackPressureDrop() is used for:

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.

onBackPressureDrop()

Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88
  • https://github.com/ReactiveX/RxJava/wiki/Backpressure#useful-operators-that-avoid-the-need-for-backpressure might also give some insights into alternatives. – Florian Barth Dec 15 '15 at 08:15
0

For RxJava 3 you can use new Flowable concept:

    observable.toFlowable(BackpressureStrategy.LATEST)

You can chose between different strategies:

    /**
     * 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
caiiiycuk
  • 1,466
  • 14
  • 20