In the model-view paradigm, this class represents an observable object, or “data.” It can be subclassed in order to represent an object that the application wishes to watch. The issue is that you’re creating software that will render data describing a three-dimensional scene in two dimensions. The application must be modular and allow for numerous, concurrent views of the same scene.
One or more observers can be assigned to an observable object. An observer can be any object that implements the Observer interface. When an observable instance changes, an application that calls the Observable’s notifyObservers() method notifies all of its observers of the change through a call to their update method.

Image 1. The Observables in RxJava Explained.
In simple terms,
- An Observable is analogous to a speaker that broadcasts the value. It does various tasks and generates some values.
- An Operator is similar to a translator in that it converts/modifies data from one form to another.
- An Observer is what fetches the value.
Observable Types in RxJava
The many types of Observables in RxJava are as follows:
- Observable
- Flowable
- Single
- Maybe
- Completable
Let’s commence creating some Observables in RxJava to better understand this,
Type #1: Creating a Basic Observable
Use Case: Assume you are downloading a file and need to push the current status of the download. You will have to emit more than one value in this case.
Kotlin
fun downloadObserervable(): Observable<Int> {
return Observable.create { gfgshooter ->
if (!gfgshooter.isDisposed) {
gfgshooter.onNext( 20 )
}
if (!gfgshooter.isDisposed) {
gfgshooter.onNext( 80 )
}
if (!gfgshooter.isDisposed) {
gfgshooter.onNext( 100 )
gfgshooter.onComplete()
}
}
}
|
Type #2: Creating a Complex Obersever using Observable
Kotlin
fun getObserver(): Observer<Int> {
return object : Observer<Int> {
override fun onStart(d: Disposable) {
editText.setText( "onStart" )
}
override fun onNext(progress: Int) {
editText.setText( "onNext : $progress" )
}
override fun onError(e: Throwable) {
editText.setText( "onError : ${e.message}" )
}
override fun onComplete() {
editText.setText( "onComplete" )
}
}
}
|
Observer <> Flowable
When the Observable emits a large number of values that cannot be absorbed by the Observer, the Flowable comes into play. In this scenario, the Observable must skip some data based on a strategy, otherwise, it will throw an exception. A strategy is used by the Flowable Observable to handle the exception. BackPressureStrategy is the strategy, and MissingBackPressureException is the exception.
Geek Tip: Just like type #1, you can create Flowable using Flowable.create() similarly.
Solitary<> SingleObserver
When an Observable must emit only one value, such as a response to a network request, Single is used.
Type #3: Creating Single Observable
Kotlin
fun singleObservable(): Single<String> {
return Single.create { emitter ->
if (!gfgEmitter.isDisposed) {
gfgEmitter.onSuccess( "Spandan's Code Ran!" )
}
}
}
|
Then use it with a single observable in the following manner:
Kotlin
fun singleObservable(): SingleObserver<String> {
return object : SingleObserver<String> {
override fun onSubscribe(d: Disposable) {
edittext.setText( "onStart" )
}
override fun onSuccess(data: String) {
editText.setText( "onSuccess : $data" )
}
override fun onError(e: Throwable) {
editText.setText( "onError : ${e.message}" )
}
}
}
|
The maybe <> Observer
When the Observable must emit a value or no value, maybe is used.
Type #4: Creating a maybe Observer
Kotlin
fun maybeObservable(): Maybe<String> {
return Maybe.create { gfgemitter ->
if (!gfgemitter.isDisposed) {
gfgemitter.onSuccess( "Spandan's Code!" )
}
}
}
|
Completable <> CompletableObserver
When the Observable must do some job without emitting a value, it is called a Completable.
Type #5: Creating a Completable Observer
Kotlin
fun completeObservable(): Completable {
return Completable.create { gfgemitter ->
if (!gfgemitter.isDisposed) {
gfgemitter.onComplete()
}
}
}
|
Using it now in the following manner:
Kotlin
fun completeObservable(): gfgObserver {
return object : gfgObserver {
override fun onStart(d: Disposable) {
editText.setText( "Started" )
}
override fun onError(e: Throwable) {
editText.setText( "onError : ${e.message}" )
}
override fun onComplete() {
editText.setText( "Done" )
}
}
}
|
Conclusion
The class combines the properties of a view (it textually shows the value of the model’s current state) with a controller (it allows the user to enter a new value for the state of the model). With help of this article, you would have now known how to create an Observable in any way you want and then use it as your procured use case.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
23 Aug, 2022
Like Article
Save Article