Lazy Emissions
Explore the concept of lazy emissions in RxJava and understand how cold Observables delay computation until subscription. Learn to use fromCallable and defer methods to wrap potentially long-running tasks, enabling efficient and asynchronous reactive programming in Android.
An intrinsic property of a cold Observable is that it is lazy. The underlying sequence is only computed and emitted on subscription time.
To demonstrate this, let’s look at another Observable creation method: .fromCallable().
Observable.fromCallable()
The Observable.fromCallable(Callable<? extends T> callable) method creates an Observable that, when subscribed to, invokes the function supplied and then emits the value returned by that function.
Constructing an Observable with .fromCallable() won’t actually do the work specified inside the Callable. All it does is define the work to be done. As such, .fromCallable() ...