Closed DCRichards closed 7 years ago
publish
turns the cold source into a hot one, similar to a PublishSubject
, and subscribeOn
has no practical effect on hot publish-like sources. In other terms, applying subscribeOn
after the ConnectableObservable
has no effect. You have to apply subscribeOn
before the publishing:
ConnectableObservable<Model> connectableObservable = doExpensiveNetworkCall()
.subscribeOn(mDataManager.getScheduler()) // <------------------------------
.publish();
// Transform in one way.
connectableObservable
.filter(...)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(...);
Looks like this question has been answered. If you have further input on the issue, don't hesitate to reopen this issue or post a new one.
Thank you @akarnokd, that was exactly what I needed, had a bit of a wood from the trees moment. Sorted 👌
Versions
RxJava: 2.1.0 RxAndroid: 2.0.1
Issue
I'm using the following pattern to allow me to share the result of an expensive network call across two observables, which both transform the result in different ways:
This is a very useful pattern, however, calling
.connect()
causesNetworkOnMainThreadException
as the expensive network call is then run on the main thread, despite all subscribers of the downstream observables being on a separate thread.Is there an equivalent of
.connectOn()
or another alternative which can be used to also let the ConnectableObservable know that it should be run on a different thread? Alternatively, if there's a better way of achieving the above, any guidance would be much appreciated!