In this article, we will learn about the RxJava Defer Operator. Depending on our use case, we'll know when to utilize the Defer operator. We frequently make mistakes when utilizing the RxJava Defer Operator. Let's get this straight so we don't make a mistake.
According to the documentation:
Defer: wait till the observer subscribes before creating the Observable, and build a new Observable for each observer.
So, in this case, we'll use an example to learn about RxJava's Defer Operator. Assume we have a GfG class, as shown below:
Kotlin
class GfG {
var course: String = "DSA"
fun getCourseObservable(): Observable<String> {
return Observable.just(course)
}
fun getCourseDeferObservable(): Observable<String> {
return Observable.defer {
return@defer Observable.just(course)
}
}
}
We have two methods in the GfG class described above:
- getCourseObservable(): This function simply returns the Observable<String> by using Observable.just(course).
- getCourseDeferObservable(): The Observable<String> is returned by using Observable.just(course), but encased within the Observable defer().
Now, let's test both methods and see what happens.
Kotlin
val course = Gfg()
val courseObservable = course.getCourseObservable()
val courseDeferObservable = course.getCourseDeferObservable()
course.price = "DSA"
courseObservable
.subscribe { brand ->
Log.d("GfGExample", "courseObservable : $course")
}
courseDeferObservable
.subscribe { brand ->
Log.d("GfGExample", "courseDeferObservable : $course")
}
First, we're going to make the price object. Then, using the price object, we use both the getCourseObservable and getCourseDeferObservable methods to retrieve the observables. Then we specify the course's marque as "DSA." Following that, we subscribe to both observables in order to determine the course's price
The Output of the Above Code:
GfGExample: courseObservable : DEFAULT
GfGExample: courseDeferObservable : DSA
Here, we can see that the courseObservable receives "DEFAULT" as the course's name, but the courseDeferObservable receives "DSA" as the course. Because the defer operator was not used, courseObservable returns the old value "DEFAULT." Because the defer operator was used, courseDeferObservable returns the most recent value, "DSA." It indicates that Defer does not generate the Observable until the observer subscribes and that each observer receives a new Observable.
Conclusion
There are two critical points to remember:
- The Observable is not created until the observer subscribes.
- For each observer, Defer generates a new observable.
As a result, we may utilize the RxJava Defer Operator to solve the intriguing problem. Hope this article helped you to understand the topic!
Similar Reads
RxJava Operator - Concat and Merge RxJava is the most significant library, and it is widely used by Android developers. It simplifies our lives. RxJava is used for multithreading, managing background processes, and eliminating callback hells. RxJava allows us to address a wide range of complicated use-cases. It allows us to accomplis
3 min read
RxJava For Android RxJava is a JVM library that uses observable sequences to perform asynchronous and event-based programming. Its primary building blocks are triple O's, which stand for Operator, Observer, and Observables. And we use them to complete asynchronous tasks in our project. It greatly simplifies multithrea
4 min read
R Operators Operators are the symbols directing the compiler to perform various kinds of operations between the operands. Operators simulate the various mathematical, logical, and decision operations performed on a set of Complex Numbers, Integers, and Numericals as input operands. R supports majorly four kinds
5 min read
Understanding RxJava Create and fromCallable Operator In this article, we will learn about the RxJava Create and fromCallable Operators. We can choose between the required function based on what is required skillset is needed. We frequently make mistakes when utilizing RxJava Operators. Let's get this straight so we don't make a mistake. With examples,
2 min read
Understanding RxJava Zip Operator With Example According to the official RxJava documentation "Zip combines the emissions of several Observables using a given function and emits single items based on the outcomes of this function for each combination". The zip operator enables us to obtain results from several observables at the same time. Image
3 min read