Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJava 2.0 - How to convert Observable to Publisher

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?

like image 808
caioquirino Avatar asked Nov 29 '16 23:11

caioquirino


People also ask

How do I extract an object from Observable?

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.

What is publisher in RxJava?

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.

What is the difference between single and Observable in RxJava?

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.


1 Answers

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.

like image 167
Yaroslav Stavnichiy Avatar answered Oct 29 '22 00:10

Yaroslav Stavnichiy