RxJava 2 : Understanding Hot vs. Cold with just vs. fromCallable

Elye
Elye
Jun 9 · 4 min read

Understanding HOT vs COLD observable seems like a hard topic someone would only want to talk about after one has a good grasp of what RxJava is.

However, when I look at just and fromCallable, found their similarity fits well, making HOT and COLD much more easily understood.

From my previous blog

Single.just(1).subscribe{ it -> print(it) } // result 1

But there’s another function… fromCallable that looks so similar, and produce same result.

Single.fromCallable{ 1 }.subscribe{ it -> print(it) } // result 1

Let’s look at another example

println("From Just")
Single.just(Random(1).nextInt()).subscribe{ it -> println(it) }
Single.just(Random(1).nextInt()).subscribe{ it -> println(it) }
println("\nFrom Callable")
Single.fromCallable { Random(1).nextInt() }
    .subscribe{ it -> println(it) }
Single.fromCallable { Random(1).nextInt() }
    .subscribe{ it -> println(it) }

The result looks like just, feel like just ….

From Just
600123930
600123930From Callable
600123930
600123930

BUT It is NOT the same as just.

Understand the differences is very important, as mention would help one to also somewhat understand the different between HOT vs COLD observable.

Let’s check out the differences…

1. Given Externally vs Generated Internally

So what’s the different? To make it clear, let’s look at the code. This time we use a random value generator that generates a different value each time it is called.

println("From Just")
val justSingle = Single.just(Random.nextInt())
justSingle.subscribe{ it -> println(it) }
justSingle.subscribe{ it -> println(it) }
println("\nFrom Callable")
val callableSingle = Single.fromCallable { Random.nextInt() }
callableSingle.subscribe{ it -> println(it) }
callableSingle.subscribe{ it -> println(it) }

We could store the Single, and use by two Consumer (subscribe twice). The result as below.

From Just
801570614
801570614From Callable
1251601849
2033593269

You’ll notice the value given by just stays the same regardless of how many subscribe it has. The value was generated externally from the Observable, and the observable just stores it for consumption later.

However for fromCallable it generates internally from the Observable. A new random number is generated each time a subscribe is connect to it.

2. Executed Immediate vs Executed Lazily

In another word, the delayed execution is also known as lazy initialization. It is something very useful in programming, to save some processing resources until it is needed later.

Let’s look at the below code.

fun main() {
    println("From Just")
    val justSingle = Single.just(getRandomMessage())
    println("start subscribing")
    justSingle.subscribe{ it -> println(it) }
   
    println("\nFrom Callable")
    val callableSingle = Single.fromCallable { getRandomMessage() }
    println("start subscribing")
    callableSingle.subscribe{ it -> println(it) }
}

fun getRandomMessage(): Int {
    println("-Generating-")
    return Random.nextInt()
}

The result is as below

From Just
-Generating-
start subscribing
1778320787From Callable
start subscribing
-Generating-
1729786515

You’ll notice the -Generating- for just is happening before start subscribing. This is because it is done first.

However for fromCallable, the -Generating- happens only after start subscribing.

An important comparison: Hot vs Cold Observable

If we also make the the table for HOT vs OLD Observable, it looks the same as above.

Hence comparing the different just and fromCallable, one could somewhat grasp it’s similarity with Hot vs Cold Observable comparison.

Note: just and fromCallable is really just a single value generator. Hence for just only one value is always generated from the same Observable. I just want to clarify, Hot Observable doesn’t means one always generate the same value; instead the value is generated externally, and not controlled by the Observable. So it could generate different values, if the Hot Observables is capable of generate sequence of values before terminating.

Another good article that describes Hot vs Cold is as below


I hope this post is helpful to you. You could check out my other interesting topics here.

Follow me on medium, Twitter, Facebook or Reddit for little tips and learning on Android, Kotlin etc related topics. ~Elye~