Zip behaviour with different schedulers in combining observables
See original GitHub issueI faced unpredictable behaviour in Zip scheduling. I have observables that supplying items in different schedulers. Zip combine function will be executed when “last” item in set will be supplied by one of observables. Also tick() and then zipFunction.call(…) will be executed in scheduler of “last” observable. It means we don’t know about scheduler where zip combining function will be executed.
For example I have a ConnectableObservable
that was started by connect()
in main thread. And then I want to combine it with other observables.
//somewhere in main thread
observable1.connect();
//somewhere else
Observable.zip(
observable1,
observable2,
(res1, res2) -> combine(res1, res2)
).subscribeOn(Schedulers.io())
Of course observable2 will be executed in the one of Schedulers.io()
threads.
But in combining function I have no idea about my current thread. It can be main thread or one of Scheduler.io()
threads. It’s quite not logical in some cases.
I suggest to add variations of zip that can operate in specified scheduler, e.g. explicitly pass scheduler to zip operator or make operator like zipMap that will be operating in scheduler you pass to whole Observable.
Any ideas?
Issue Analytics
- State:
- Created 9 years ago
- Comments:11 (2 by maintainers)
Hi @franciscojunior
Can you try specifying the scheduler for which a second
Single
is operating on? e.g.or if you use static method version
From my observation, I think
zip
operator’s combine function is being called in a scheduler that the source singles are operating on. If the sources are operating on different scheduler, it is quite uncertain as to which scheduler will be used to run the combine function. I guess it will be a scheduler of a single that emit the latter item for each combination of a zipped items. By specifyingmainThreadScheduler
to both singles, it will ensure that the combine function will only be run from the main thread.Hi @franciscojunior
You may try to use
.observeOn()
on your second observable instead of.subscribeOn()
i.e.No matter what scheduler a
Single
is originally operating on, theobserveOn()
will instruct it to emit an item on a particular scheduler.http://reactivex.io/documentation/operators/observeon.html