Using Flowable.generate()

Despite a lot of the content that has been covered so far in this chapter, we have not yet demonstrated the optimal approach to apply backpressure to a source. Although the standard Flowable factories and operators automatically handle the backpressure, the onBackPressureXXX() operators, while quick and effective for some cases, just cache or drop emissions, which is not always desirable. It would be better to force the source to slow down as needed in the first place.

Thankfully, Flowable.generate() exists to help create backpressure, respecting sources at a nicely abstracted level. It accepts a Consumer<Emitter<T>>, much like Flowable.create(), but uses a lambda to specify which onNext(), onComplete(), and onError() ...

Get Learning RxJava - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.