What is the difference between Observable, Completable and Single in RxJava
Observable
is the generic ReactiveX building block, of event source that emits values over time. (and thus exists in every language ReactiveX extended to)
in short Observable events are:
onNext* (onCompleted | onError)? /(* zero or more ? - zero or 1)
Single
and Completable
are new types introduced exclusively at RxJava that represent reduced types of Observable
, that have more concise API.
Single
represent Observable
that emit single value or error.
Completable
represent Observable
that emits no value, but only terminal events, either onError
or onCompleted
You can think of the differences like the differences of a method that returns:
Collection of Objects - Observable
Single object - Single
and method that returns no values (void method) - Completable.
Single
can be appropriate when you have task oriented Observable and you expect single value, like Network request which is performed once and return value (or error), network call is operated in one time fashion, meaning you don't expect it to return additional values over time. Another example is DB fetch data operation.
Completable
is appropriate when you have an Observable
and you don't care about the value resulted from the operation, or there isn't any.
Examples are updating a cache for instance, the operation can either succeed/fail, but there is no value.
Another example is some long running init operation that doesn't return anything. It can be UPDATE/PUT network call that resulted with success indication only.
In any case, Completable and Single are not adding new capabilities but they are introducing more robust and concise APIs, that tells more about the operations behind the Observable that the API exposed.
Edit:
RxJava2 Maybe
:
RxJava2 added a new type called Maybe
, Maybe
is the combination of Completable
and Single.
In the same language like above, Maybe
can be thought of as a method that returns
Optional
of some type, Optional
is a wrapper around Object that explicitly tells whether we have some value in it - Object
or not (instead of null).
With Maybe
we can either have some value exactly like Single
or have return nothing - just like Completable
. Additionally, like both, we have the error.Maybe
is valuable when we want to mark that an Observable
might not have a value and will just complete.
An example would be fetched from the cache, we'll not necessarily have a value in the cache, so in this case, we will complete, o.w. we will get onNext
with the value from the cache.
This is also worthy to handle non-null values in a stream with RxJava2.
RxJava2 Flowable
:
First, let's define backpressure. Backpressure is a means of handling the situation where data is generated faster than it can be processed. Flowable
has backpressure support allowing downstream to request items. You can read more about the differences here.
Single & SingleObserver
Single
always emits only one value or throws an error. Single
always makes sure there is an emission.
A use case of Single would be making a network call to get a response as the response will be fetched at once.
Notice here, the SingleObserver
doesn’t have onNext() to emit the data, instead the data will be received in the onSuccess(Note note)
method.
Completable & CompletableObserver
Completable
observable won’t emit any data instead it notifies the status of the task either success
or failure
. This observable can be used when you want to perform some task and not expect any value.
A use case would be updating some data on the server by making a PUT request.
- Flowable and Observable can represent finite or infinite streams. Flowable support back-pressure.
- Single are streams with a single element.
- Maybe are streams with either 0 or one element.
- Finally a Completable represents a stream with no elements, i.e it can only complete without a value or fail.
A concise answer I found here on RXJava section.