Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to buffer only latest emission from rx.Observable during backpressure

I have an rx.Observable which emits the progress of a task to onNext(). The onNext() emissions can sometimes occur so quickly that the Observer cannot keep up, resulting in backpressure. I would like to handle the backpressure by only buffering the latest emission from the Observable.

For example:

  • Observable emits 1 and Observer receives 1.
  • While Observer is still processing 1, Observable emits 2, 3, and 4.
  • Observer finishes processing 1 and begins processing 4 (emissions 2 and 3 are dropped).

This seems like it would be a common case for handling progress in an Rx Observable since you usually only care about updating your UI with the latest progress information. However I have not been able to figure out how to do this.

Anyone know how this can be achieved with RxJava?

like image 405
ashughes Avatar asked Aug 04 '15 17:08

ashughes


1 Answers

onBackPressureLatest is your friend here. :) http://reactivex.io/RxJava/javadoc/rx/Observable.html#onBackpressureLatest()

like image 100
lopar Avatar answered Nov 14 '22 01:11

lopar