How to convert Observable to Publisher in RxJava version 2?
In the first version we have the https://github.com/ReactiveX/RxJavaReactiveStreams project that do exactly what I need. But How can I do it in RxJava 2?
You cannot 'extract' something from an observable. You get items from observable when you subscribe to them (if they emit any). Since the object you are returning is of type Observable, you can apply operators to transform your data to your linking.
It's building on the Observable design pattern, which means that your building blocks can either be of type Publisher or Subscribers. A Publisher is an object that can produce events and a Subscriber can consume events produced by a Publisher.
Observable: emit a stream elements (endlessly) Flowable: emit a stream of elements (endlessly, with backpressure) Single: emits exactly one element. Maybe: emits zero or one elements.
Use the following code:
Publisher publisher = observable.toFlowable(BackpressureStrategy.XXX);
As Observable
does not implement backpressure, you need to select backpressure strategy when converting. See available choices here.
Or use Flowable
instead of Observable
in the first place. See here for details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With