RxJava Create and fromCallable Operator
- Authors
- Name
- Amit Shekhar
- Published on
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 are going to learn about the RxJava Create and fromCallable Operators. We will understand when to use the Create operator and when to use the fromCallable operator depending on our use case. Most of the time, we do mistakes while using RxJava Operators. Let's understand it clearly to avoid the mistake.
We will learn the following operators with examples.
- Create
- fromCallable
Create Operator of RxJava
Create Operator: create an Observable from scratch by means of a function.
Using Create Operator, we can do a task and keep emitting values one by one and finally completes. Let's see with an example
Observable.create<String> { emitter ->
// do something and emit first item
if (!emitter.isDisposed) {
emitter.onNext("One")
}
// do something and emit second item
if (!emitter.isDisposed) {
emitter.onNext("Two")
}
// on complete
if (!emitter.isDisposed) {
emitter.onComplete()
}
}
.subscribeOn(Schedulers.io())
.subscribe { item ->
Log.d("CreateExample", "item : $item")
}
It will print "One" and "Two".
fromCallable Operator of RxJava
fromCallable Operator: Create an Observable from scratch but can emit only one item means fromCallable return an item.
Using fromCallable, we can do a task and can emit only one item. Let's see with an example
Observable.fromCallable<String> {
// do something and return
return@fromCallable "Amit"
}
.subscribeOn(Schedulers.io())
.subscribe { item ->
Log.d("FromCallableExample", "item : $item")
}
It will print "Amit".
This does not mean that fromCallable is like Single. We will see later how it actually differs.
Both Create and fromCallable defer the execution of the task we specify until an observer subscribes to the ObservableSource. Means, it makes the task "lazy."
So, the following are the major differences between Create and fromCallable Operators:
- Create can emit multiple items whereas fromCallable can emit only one item.
- There is no simple way to check if isDisposed in fromCallable as present in Create. So if it emits item after disposal, the throwable is delivered to the global error handler via RxJavaPlugins.onError as UndeliverableException. It means it will crash the application. This is how it is different from Single.
This way we can use RxJava Create and fromCallable Operators 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: