Dropping items

Dropping means that if the downstream processing steps cannot keep up with the pace of the source Observable, they will just drop the data that cannot be handled. This can only be used in cases when losing data is okay, and you care more about the values that were emitted in the beginning.

There are a few ways in which items can be dropped.

The first one is just to specify Backpressure strategy, like this:

observable.toFlowable(BackpressureStrategy.DROP)

Alternatively, it will be like this:

observable.toFlowable(BackpressureStrategy.MISSING)
.onBackpressureDrop()

A similar way to do that would be to call .sample(). It will emit items only periodically, and it will take only the last value that's available (while BackpressureStrategy.DROP drops it instantly unless it is free to push it down the stream). All the other values between ticks will be dropped:

observable.toFlowable(BackpressureStrategy.MISSING)
.sample(10, TimeUnit.MILLISECONDS)
.observeOn(Schedulers.computation())
.subscribe(v -> log("s", v.toString()), this::log);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.78.136