How Observable works

As we stated earlier, an Observable value has the following three most important events/methods:

  • onNext: The Observable interface passes all the items one by one to this method
  • onComplete: When all the items have gone through the onNext method, the Observable calls the onComplete method
  • onError: When the Observable faces any error, it calls the onError method to deal with the error, if defined

One thing to note here is that the item in Observable that we are talking about can be anything; it is defined as Observable<T>, where T can be any class. Even an array/list can be assigned as Observable.

Let's look at the following diagram:

Here's a code example to understand it better:

fun main(args: Array<String>) { 
 
    val observer = object :Observer<Any>{//1 
    override fun onComplete() {//2 
        println("All Completed") 
    } 
 
        override fun onNext(item: Any) {//3 
            println("Next $item") 
        } 
 
        override fun onError(e: Throwable) {//4 
            println("Error Occured $e") 
        } 
 
        override fun onSubscribe(d: Disposable) {//5 
            println("Subscribed to $d") 
        } 
    } 
 
    val observable = listOf(1, "Two", 3, "Four", "Five", 5.5f).toObservable() //6 
 
    observable.subscribe(observer)//7 
 
    val observableOnList = Observable.just(listOf("One", 2, "Three", "Four", 4.5, "Five", 6.0f), 
            listOf("List with 1 Item"), 
            listOf(1,2,3))//8 
 
 
 
    observableOnList.subscribe(observer)//9 
} 

In the preceding example, we declared the observer instance of the Any datatype at comment 1.

Here, we take the benefit of the Any datatype. In Kotlin, every class is a child class of Any. Also, in Kotlin, everything is a class and object; there is no separate primitive datatype.

The Observer interface has four methods declared in it. The onComplete() method at comment 2 gets called when Observable is finished with all its items without any error. At comment 3, we defined the onNext(item: Any) function, which will be called by the observable value  for each item it has to emit. In that method, we printed the data to the console. At comment 4, we defined the onError(e: Throwable) method, which will be called in case the Observable interface faces an error. At comment 5, the onSubscribe(d: Disposable) method will get called whenever the Observer subscribes to Observable. At comment 6, we created an Observable from a list (val observable) and subscribed to the observable value with the observer value at comment 7. At comment 8, we again created observable (val observableOnList), which holds lists as items.

The output of the program is as follows:

So, as you can see in the output, for the first subscription (comment 7), when we subscribe to the observable value, it calls the onSubscribe method, then the Observable property starts emitting items, as observer starts receiving them on the onNext method and prints them. When all the items are emitted from the Observable property, it calls the onComplete method to denote that all the items have been successfully emitted. It is the same with the second one, except that here, each item is a list.

As we gained some grip on Observables, you can now learn a few ways to create Observable factory methods for Observable.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.71.21