F
F
foonfyrick2021-02-07 12:03:34
Android
foonfyrick, 2021-02-07 12:03:34

RxJava2 onBackpressureBuffer() action, parameters, buffer?

In general, there is such code:

MyRetrofit.api.getPostsFlowable()
            .onBackpressureBuffer()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
            Log.e("onFlowable",it.toString())
        }

I get a list of data from https://jsonplaceholder.typicode.com/posts
I log this entire list.
In this example, I'm interested in the onBackpressureBuffer() line, because no matter how hard I try, I don't see how it works.
I read that a buffer is a linear sequence of data of a primitive type, as I understand it, this is the data that I output. In observeOn, by default, the buffer = 128 (something there, I didn’t understand what), and so, in onBackpressureBuffer(), you can set the following parameters
: If I didn't put a number, nothing appears anywhere.
2) BackpressureOverflowStrategy - a strategy that determines the behavior when the specified buffer capacity is reached.
So, how do I edit the code above so that I can see what happens if the buffer reaches the specified mark? How can I overflow it so that I can see the difference with and without .onBackpressureBuffer()?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2021-02-07
@foonfyrick

Backpressure is a situation where the subscriber (consumer) consumes (processes) values ​​more slowly than the emitter (producer) produces them. Then the events, of course, cannot go anywhere, they must be copied to the buffer, dropped (drop), or immediately throw an error when backpressure occurs. In your situation, obviously no backpressure can happen, because you just take data from the network (for a long time) and output it to the log (quickly).
You can simulate long processing by making an artificial delay (Thread.sleep) and making a fast emit. For example, for clarity, create a flowable, and throw 10,000 elements into it in a loop. And in the subscript - slip.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question