What is the difference between Observable, Completable and Single in RxJava

Raja Jawahar picture Raja Jawahar · Mar 13, 2017 · Viewed 26.6k times · Source

Can anyone please explain the difference between Observable, Completable and Single in RxJava with clear examples?

In which scenario we use one over the others?

Answer

yosriz picture yosriz · Mar 13, 2017

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.

Completableis 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.