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

3

143

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?

Frias answered 13/3, 2017 at 6:50 Comment(1)
This is a perfectly valid question and should not have been closed. Although rx is a great library, the documentation and examples are unfortunately extremely poor and confusing.Lewse
S
302

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.

Sumerlin answered 13/3, 2017 at 8:42 Comment(3)
What about flowable?Sicken
So if I am writing in Kotlin, is Maybe<T>, essentially, Single<T?> ?Chirography
Can you consider Single to be a Completable with returned value?Bernardinebernardo
R
25
  1. Flowable and Observable can represent finite or infinite streams. Flowable support back-pressure.
  2. Single are streams with a single element.
  3. Maybe are streams with either 0 or one element.
  4. 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.

Rode answered 4/6, 2018 at 5:50 Comment(0)
N
2

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.

Nerves answered 1/9, 2020 at 19:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.