What is the difference between observer and subscriber?

I am trying to decrypt the following function:

Subscription getCar(id, Observer<Car> observer) { return getCarDetails(id, new Observer<CarDetails> { @Override onNext(CarDetails details) { observer.onNext(details.getCar()); } }); } 

I got a good introduction to rxjava from http://blog.danlew.net/2014/09/15/grokking-rxjava-part-1/ , but he only mentioned Observer, saying that you will most often use Subscriber for consumer goods emitted by the Observed.

Can someone explain to me

  • What is an observer?
  • How is an observer different from a subscriber?
  • What does the above code snippet do?

Javadoc made him seem like just a subscriber. Javadoc for the subscriber says that it implements the observer and the subscription. I am very confused.

+55
java rx-java
Dec 27 '14 at 4:03
source share
4 answers

EDITED : with comment by @Alrid

TL; DR

 public abstract class Subscriber<T> implements Observer<T>, Subscription 

So, Subscriber is an implementation of Observer , with additional subscription semantics (more about non-subscription). The code in your question just shows that it passes the Observer interface instead of the implementation (common programming practice).

This code also returns a Subscription , which may be due to the fact that the author of this code believed that the client should have access only to the Subscription methods without access to the elements created by the observables. This may be a programmer error.

long story

Indeed, you should read the contents of this website (or book): http://www.introtorx.com It's about Rx.Net, but the concepts are the same, they were created by Eric Majer and the RxJava developers who follow them (if Applicable to Java language).

This page will interest you (this is the second chapter): KeyTypes

Here you will read in the first paragraphs:

There are two key types to understand when working with Rx, and a subset of helper types to help you learn more about Rx. IObserver and IObservable form the basic building blocks for Rx, and ISubject implementations reduce the learning curve for developers new to Rx.

...

Essentially, Rx is built around the Observer..NET pattern already provides some other ways to implement the Observer pattern, such as multicast delegates or events (which are usually multicast delegates).

Even if the types / APIs are slightly different, you will learn a lot with this book, perhaps more than with some blogs.

What this book does not say (... because it is in the implementation of RxJava)

The main RxJava developer introduced a small change at this time (see PR # 792 ), which allowed distinguishing between two types of contracts:

  • notice → Observer
  • (un) subscription → Subscription

This change allowed us to better express / share these problems with the implementing classes of the RxJava library.

However, as a library user, using actual RxJava library implementations should be good enough.

The implementation of the subscriber requires much more knowledge, work and care, indeed, the semantics of the subscription is very important depending on the type of source observed (hot or cold? Expensive to create?)




Identifying Subscriber rather than Observer in such cases as indicated above will not interfere with the code in most cases, but this is not intended for it if semantics are needed without subscription. But in the end, the implementation of Subscriber may include some pitfalls, such as:

  • spend resources on functionality that you will not use
  • cannot inherit another class
  • write the wrong code without a subscription
  • copy / paste the code with the wrong code or the correct code written for a different context.
+48
Dec 27 '14 at 12:35
source share

(Edit: this seems to be true only for RxJava 1.)

  • An Observer is an object that can receive data from a data source ( Observable ). The data source pushes data toward it, invoking the onNext() observer.

  • A Subscriber is an Observer that can also unsubscribe from this data source (via the Subscription interface).

  • The getCar() function tries to return the cars, but there is no direct method for this. But there is a function to get the details of the car ( getCarDetails() ), which will call an observer with all the details of the car. Therefore, he calls this function and passes it to the observer, who, when he receives the data, will extract the vehicle data from the parts and transmit it to his own observer.

+23
Oct 06 '15 at 5:20
source share

In RxJava 2, org.reactivestreams.Subscriber is an interface that conforms to the Reactive Threads specification.

The main difference from Observable is that the new Subsciber supports backpressure.

Observer gets hooked to Observable , and Subscriber subscribes to Flowable (implements org.reactivestreams.Publisher ).

Detailed description here .

+11
Jan 13 '17 at 12:29
source share

Also in RxJava2 , if you want to unsubscribe, you must use ResourceObserver for Observable and ResourceSubscriber for Flowable .

Check this question

+1
Jun 16 '17 at 23:00
source share



All Articles