RxJava Defer Operator

Authors
  • Amit Shekhar
    Name
    Amit Shekhar
    Published on
RxJava Defer Operator

I am Amit Shekhar, Co-Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.

Join Outcome School and get high paying tech job: Outcome School

Before we start, I would like to mention that, I have released a video playlist to help you crack the Android Interview: Check out Android Interview Questions and Answers.

In this blog, we will learn about the Defer operator of RxJava. We will understand when to use the Defer operator depending on our use case. Most of the time, we do mistakes while using the RxJava Defer Operator. Let's understand it clearly to avoid the mistake.

As per documentation:

Defer: do not create the Observable until the observer subscribes, and create a fresh Observable for each observer

So here, we will be taking an example to learn the Defer Operator of RxJava.

Let say we have a class Car as below:

class Car {

    var brand: String = "DEFAULT"

    fun getBrandObservable(): Observable<String> {
        return Observable.just(brand)
    }

    fun getBrandDeferObservable(): Observable<String> {
        return Observable.defer {
            return@defer Observable.just(brand)
        }
    }

}

In the above Car class, we have two methods as below:

  • getBrandObservable(): It returns the Observable<String> by just using Observable.just(brand).
  • getBrandDeferObservable(): It returns the Observable<String> by using Observable.just(brand) but wrapped inside the Observable.defer().

Now, let's try to use both the method and see the difference.

val car = Car()

val brandObservable = car.getBrandObservable()
val brandDeferObservable = car.getBrandDeferObservable()

car.brand = "BMW"

brandObservable
.subscribe { brand ->
    Log.d("DeferExample", "brandObservable : $brand")
}

brandDeferObservable
.subscribe { brand ->
    Log.d("DeferExample", "brandDeferObservable : $brand")
}

Here, first, we are creating the car object. Then using the car object, we are calling both the methods which are getBrandObservable and getBrandDeferObservable to get the observables. Then, we are setting the brand of car as "BMW". After that, we are subscribing to both the observables to get the brand of the car.

It will print the following:

DeferExample: brandObservable : DEFAULT
DeferExample: brandDeferObservable : BMW

Here we can see that the brandObservable is getting the "DEFAULT" as the brand of the car and the brandDeferObservable is getting "BMW" as the brand of the car.

brandObservable is returning the old value which is "DEFAULT" as it has not used the defer operator.

brandDeferObservable is returning the latest value which is "BMW" as it has used the defer operator.

It means that Defer does not create the Observable until the observer subscribes, and create a fresh Observable for each observer.

Two important things to keep in mind:

  • Defer does not create the Observable until the observer subscribes.
  • Defer creates a fresh observable for each observer.

This way we can use RxJava Defer Operator to solve the interesting problem.

Find the complete project here and learn RxJava.

Prepare yourself for Android Interview: Android Interview Questions

That's it for now.

Thanks

Amit Shekhar
Co-Founder @ Outcome School

You can connect with me on:

Follow Outcome School on:

Read all of our high-quality blogs here.