Iterator<T>.toObservable
Thanks to the extension functions of Kotlin, you can turn any Iterable
instance, such as list
, to Observable
without much effort. We have already used this method in Chapter 1, Kotlin – Data Types, Objects, and Classes, but again take a look at this:
fun main(args: Array<String>) { val observer: Observer<String> = object : Observer<String> { override fun onComplete() { println("Completed") } override fun onNext(item: String) { println("Received-> $item") } override fun onError(e: Throwable) { println("Error Occured => ${e.message}") } override fun onSubscribe(d: Disposable) { println("Subscription") } }//Create Observer val list:List<String> = listOf("Str 1","Str 2","Str 3","Str 4") val observable: Observable<String> = list.toObservable() observable.subscribe(observer...