Observable (Observable’s just method) With Simple Example

In this post under RxJava, I will explain what is an Observable with an example.

An Observable is a push based stream of data/events. In other words an Observable pushes items of type T directly to the observer or indirectly to an observer through a chain of operators.

Types of Observable

They are four types of Observable in RxJava

  1. Observable: This is stream of data/events, which at any point in time generates either onNext or (onComplete or onError) events. In RxJava, this observable is represented by Observable class.
    • onNext(): event generated when items are passed to next operator or observer
    • onComplete(): communicates the end of stream to next operator or observer
    • onError(): communicates the occurrence of error to next operator or observer
  2. Single: This observable emits a single item or an error. On emit of an item onSuccess event is generated and when error is thrown onError event is generated. This observable represents a computation task that can emit an item or error.
  3. Maybe: This observable emits a single item, or no item at all, or an error. On emit of an item onSuccess event is generated, on no item emit onComplete is generated, and when an error is thrown onError event is generated. This observable represents a computation task that can emit an item, or no item, or an error.
  4. Completable: This observable emits completion event or error event. On completion onComplete event is generated and when an error is thrown onError event is generated. This observable represents a computation task that doesn’t emit an item just signifies a completion or error event.

In this post we will create an Observable with “just” method. The “just” method takes minimum one or max 10 items and convert them into an Observable of items.

Below is an example of simple Observable

Example


1 import io.reactivex.rxjava3.core.Observable;
2 
3 public class Example1 {
4     public static void main(String[] args) {
5         Observable<String> observable = Observable.<String>just("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
6         observable.subscribe(t -> System.out.println(t));
7     }
8 }

In the above example, at line 5, we have created an Observable of items of type String using the “just” method. We also passed the items as arguments to the “just” method.

Any type of Observable created, doesn’t start emitting items until an Observer subscribes to it. The moment an Observer subscribes to it, the Observable starts emitting items. In other words, all an Observable instance requires to emit items is atleast one Observer subscribing to it.

An Observer subscribes to an Observable instance by calling subscribe method as done in the above code at line 6. The subscribe method shown in the above code takes only one callback function, but usually we need to pass three callback functions. The first callback function is executed when onNext event is generated by the Observable, the second callback function is executed when onError event is generated by the Observable, and third one is executed when onComplete event is generated by the Observable.

The subscribe method takes a callback function provided by the user and internally creates an Observer instance, creates a stream linking the Observer instance with the Observable instance and gives the callback function to the Observer instance. Based on the type of event generated by the Observable instance, the Observer instance will call appropriate callback function.

At line 6, we subscribe to an Observable by calling the “subscribe” method, and passing only one callback functionality as an argument to “subscribe” method. This callback function is called whenever an item is sent from the Observable generating “onNext” event as explained before.

In our example this callback function simply prints the item sent.

Leave a Reply