Composing operators with transformer
So, you have learned how to create custom operators, but think of a situation when you want to create a new operator by combining multiple operators. For instance, I often wanted to combine the functionality of the subscribeOn
and observeOn
operators so that all the computations can be pushed to computation threads, and, when the results are ready, we can receive them on the main thread.
Yes, it's possible to get the benefits of both operators by adding both operators one after the other to the chain, as shown here:
fun main(args: Array<String>) { Observable.range(1,10) .map { println("map - ${Thread.currentThread().name} $it") it } .subscribeOn(Schedulers.computation()) .observeOn(Schedulers.io()) .subscribe { println("onNext - ${Thread.currentThread().name} $it") } runBlocking { delay(100) } }
Though you're already aware of...