RxJava- How to flat map backpressure ()

Perhaps I am losing sight of a simple combination of statements (or the inherent behavior of RxJava in general). But suppose I have a hot observable selectedItem that maps to an RxJava-JDBC request.

 @Test public void testFlatMapBackPressure() { Database db = null; //assign db BehaviorSubject<Integer> selectedItem = BehaviorSubject.create(); //can I backpressure the queries so only the latest one is running, and any previous is cancelled? Observable<List<Integer>> currentValues = selectedItem.flatMap(i -> db.select("SELECT VALUE FROM MY_TABLE WHERE ID =?") .parameter(i) .getAs(Integer.class) .toList()); } 

How can I skip the flatMap() operator so that it ALWAYS only flatMap() last request (and cancels the previous ones). I kind of want the flatMap operator with slippage to do something like this, where โ€œXโ€ means canceling the previous request

enter image description here

Is there any way to do this? Or can it be done already, and I just don't see it?

+5
source share
1 answer

It looks like you need switchMap() instead of flatMap() .

Returns a new Observable, applying the function that you supply to each element emitted by the Observable source, which returns the Observable, and then emits the elements emitted by the last of these observables.

enter image description here

+6
source

Source: https://habr.com/ru/post/1232287/


All Articles