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

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

I show a very simple example of using just used in RxJava to provide a value to my Single producer object.

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

But there’s another function… fromCallable that looks so similar, and produce the 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
600123930
From Callable…

--

--