I am trying to decipher 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 intro to rxjava from http://blog.danlew.net/2014/09/15/grokking-rxjava-part-1/ but it only mentioned Observer in passing, saying you'll be using Subscriber most of the time to consumer items emitted from an Observable.
Can someone explain to me
- What is an observer?
- How is an observer different than a subscriber?
- What does the above code snippet do?
Javadoc made it seem just like a subscriber. The javadoc for subscriber says it implements observer and subscription. I am very confused.
EDITED: with @Alrid's comment
tl;dr
So a Subscriber is an implementation of the Observer, with additional semantics on subscription (it's more about un-subscription). The code in your question just shows that it passes the
Observer
interface, instead of the implementation (usual programming practice).Also this code returns a
Subscription
, that may be because the author of this code thought that the client should only have access toSubscription
methods, without access to elements produced by the observable. That may be a programmer error.long story
Really you should read the content of this website (or book) : http://www.introtorx.com It is about Rx.Net, but the concepts are the very same, they were created by Erik Meijer and RxJava implementors followed them (if applicable to the Java language).
This page will interest you (it is the second chapter) : KeyTypes
Here you'll read in the first paragraphs :
...
Even if types / API are a bit different, you will learn a lot with this book, probably way more than with some blogs.
What this book do not say (...because it is in the RxJava implementation)
RxJava main developer at this time introduced a slight variation (see PR #792) that allowed to distinguish two types of contracts :
Observer
Subscription
This change allowed to better express/split these concerns of the implementing classes of the RxJava library.
However as a library user, using actual implementations of the RxJava library should be good enough.
Implementing a subscriber require much more knowledge, work and care, indeed the subscription semantics are very important depending on the type of the source observable (Hot or cold? Expensive to create ?)
Exposing
Subscriber
rather thanObserver
in cases such as above will not interfere with the code in in most cases, but it is not the intended use for it unless those un-subscription semantics are needed. But in the end implementing aSubscriber
, and may involve to fall in some pitfalls such as :(Edit: This is apparently only true of RxJava 1.)
An
Observer
is an object that can get data from a data source (anObservable
). The data source pushes data to it by calling the observer'sonNext()
.A
Subscriber
is anObserver
that can also unsubscribe from that data source (through theSubscription
interface).The
getCar()
function is trying to return cars, but there's no direct method to do that. But there is a function to get car details (getCarDetails()
) which will call an observer with all the car details. So it calls that function and passes it an observer that, when it gets data, will fetch the car data from the details and pass it on to its own observer.In RxJava 2
org.reactivestreams.Subscriber
is an interface complying to Reactive Streams specification.The main difference from
Observable
is that newSubscriber
supports backpressure.Observer
is subscibed toObservable
, andSubscriber
is subscribed toFlowable
(implementsorg.reactivestreams.Publisher
).See detailed description here.
Also in RxJava2, if you want to be able to unsubscribe, you should use
ResourceObserver
forObservable
andResourceSubscriber
forFlowable
.Check this question