ursus
07/04/2018, 12:32 AMclass AudioRouter {
init {
disposables += toggleRelay
.scan<AudioRoute>(AudioRoute.DEFAULT) { last, _ ->
if (last === AudioRoute.DEFAULT)
AudioRoute.SPEAKER
else
AudioRoute.DEFAULT
}
.subscribe(audioRouteRelay)
disposables += audioRouteRelay
.map {
if (it === AudioRoute.SPEAKER)
AudioDevice.SPEAKER
else
AudioDevice.EARPIECE
}
.subscribe {
when (it) {
AudioDevice.SPEAKER -> {
longerBlockingMethod1()
}
AudioDevice.EARPIECE -> {
longerBlockingMethod2()
}
}
}
}
fun audioRouteObservable(): Observable<AudioRoute> = audioRouteRelay
}
AudioRouter sits in a service which is on HandlerThread. Ui subscribes to audioRouteObservable() and observesOn mainthread. I there map AudioRoute to button activated or not activated
My issue is, that, when I toggle, button gets doesnt get activated "right away" as I would expect, but few ms later, as if longerBlockingMethod() was blocking the emission. If I put observeOn computation to audioRouteRelay in AudioRouter, the problem is no longer there. So longerBlockingMethod is the cause.
This is my question, why is that? Why doesnt it refresh the UI first and then those blocking methods?
Now that I think of it, Is it order of subscription issue? Is this internal subscriber first, so he firstly gets the emission after toggle emits, and UI subscribes second, therefore it is blocked?