Observable defer method

In this post under RxJava, I will show with example how to create an Observable instance with the help of Observable class’s static method “defer”.

In all my previous post, we used to create an Observable instance immediately for example


    Observable<Long> observable = Observable.interval(500, TimeUnit.MILLISECONDS);

When the above code snippet is executed, it creates an Observable instance whose job is to emit numbers with time gap of 500 milliseconds between each emission. The emission will not start until we subscribe with an Observer but the Observable instance is already created.

We can postpone the creation of Observable instance itself until a Observer subscribes to it. This can be done with the help of “defer” static method.

The defer method takes a factory class instance as an argument and return an Observable instance which will act as a placeholder for the real Observable instance.

When the Observer subscribe to the placeholder Observable, the actual Observable instance is created by calling the factory class.

I will show with an example. For our example we will create a factory class as shown below

Below is an example

ObservableSupplier


1  class ObservableSupplier implements Supplier<Observable<Integer>> {
2      private int capacity;
3      
4      @Override
5      public @NonNull Observable<Integer> get() throws Throwable {
6          return Observable.range(0, capacity);
7      }
8  
9      public int getCapacity() {
10         return capacity;
11     }
12 
13     public void setCapacity(int capacity) {
14         this.capacity = capacity;
15     }
16 }

This factory class creates an Observable instance that prints numbers from 0 to the value mentioned in the capacity variable. The value of capacity variable can be changed at runtime with the help of setter method.

Below is the main class showing the usage of defer method

Main class


1  import io.reactivex.rxjava3.annotations.NonNull;
2  import io.reactivex.rxjava3.core.Observable;
3  import io.reactivex.rxjava3.functions.Supplier;
4  
5  public class Example6 {
6      public static void main(String[] args) {
7          ObservableSupplier observableSupplier = new ObservableSupplier();
8          Observable<Integer> observable = Observable.defer(observableSupplier);
9          
10         observableSupplier.setCapacity(10);
11         observable.subscribe(t -> System.out.print(t + ", "));
12         System.out.println();
13         observableSupplier.setCapacity(20);
14         observable.subscribe(t -> System.out.print(t + ", "));
15     }
16 }

In the above code, at line 7, we create the factory instance “observableSupplier”. This factory class creates the real Observable instance.

At line 8, we call the “defer” method passing the factory instance as an argument. This method return an Observable instance which acts as a placeholder for real
Observable instance.

Now at line 10, we set the capacity to 10 and at line 11 we call subscribe method. As a result of which, the actual Observable instance with a range of 0 to 10 is created, which emits each number to Observer which prints the value in the console.

Similarly at line 13, we set the capacity to 20 and at line 14 we call subscribe method. As a result of which, the actual Observable instance with a range of 0 to 20 is created, which emits each number to Observer which prints the value in the console.

Below is the output

Output

0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,

Leave a Reply