我有一个ObservableTransformer,里面有一些计算。我想在订阅之前使用subscribeOn(Schedulers.io)
然后observeOn(AndroidSchedulers.mainThread)
,但如果我在订阅运算符之前使用它,则会发生错误(IllegalStateException: Expected to be called on the main thread
)。如何在工作线程上进行此订阅并在MainThread上进行观察?这就是我的代码的样子:
Observable
.compose(event())
.subscribe()
ObservableTransformer<Event,Model> event(){
return events -> events
.publish(shared -> Observable.merge(
shared.ofType(EventModel.class)
.compose(action()),
shared.ofType(EventModel1.class)
.compose(action1()))
.scan(initialState,(state,result) -> {
//computation here
});
}
编辑:
我试过用这个:
<T> ObservableTransformer<T, T> applySchedulers() {
return observable -> observable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
Observable
.compose(event())
.compose(applySchedulers())
.subscribe()
我在博客中找到但仍返回illegalStateExecption
编辑2:
这是stacktrace:
java.lang.IllegalStateException: Expected to be called on the main thread but was RxCachedThreadScheduler-1
at com.jakewharton.rxbinding2.internal.Preconditions.checkMainThread(Preconditions.java:35)
at com.jakewharton.rxbinding2.support.v4.widget.SwipeRefreshLayoutRefreshObservable.subscribeActual(SwipeRefreshLayoutRefreshObservable.java:20)
at io.reactivex.Observable.subscribe(Observable.java:11040)
at io.reactivex.internal.operators.observable.ObservableObserveOn.subscribeActual(ObservableObserveOn.java:45)
at io.reactivex.Observable.subscribe(Observable.java:11040)
at io.reactivex.internal.operators.observable.ObservableMap.subscribeActual(ObservableMap.java:33)
at io.reactivex.Observable.subscribe(Observable.java:11040)
at io.reactivex.internal.operators.observable.ObservableConcatMap$ConcatMapDelayErrorObserver.drain(ObservableConcatMap.java:469)
at io.reactivex.internal.operators.observable.ObservableConcatMap$ConcatMapDelayErrorObserver.onSubscribe(ObservableConcatMap.java:324)
at io.reactivex.internal.operators.observable.ObservableFromArray.subscribeActual(ObservableFromArray.java:30)
at io.reactivex.Observable.subscribe(Observable.java:11040)
at io.reactivex.internal.operators.observable.ObservableConcatMap.subscribeActual(ObservableConcatMap.java:54)
at io.reactivex.Observable.subscribe(Observable.java:11040)
at io.reactivex.internal.operators.observable.ObservableFlatMap$MergeObserver.subscribeInner(ObservableFlatMap.java:162)
at io.reactivex.internal.operators.observable.ObservableFlatMap$MergeObserver.onNext(ObservableFlatMap.java:139)
at io.reactivex.internal.operators.observable.ObservableFromArray$FromArrayDisposable.run(ObservableFromArray.java:107)
at io.reactivex.internal.operators.observable.ObservableFromArray.subscribeActual(ObservableFromArray.java:36)
at io.reactivex.Observable.subscribe(Observable.java:11040)
at io.reactivex.internal.operators.observable.ObservableFlatMap.subscribeActual(ObservableFlatMap.java:55)
at io.reactivex.Observable.subscribe(Observable.java:11040)
at io.reactivex.internal.operators.observable.ObservableFlatMap$MergeObserver.subscribeInner(ObservableFlatMap.java:162)
at io.reactivex.internal.operators.observable.ObservableFlatMap$MergeObserver.onNext(ObservableFlatMap.java:139)
at io.reactivex.internal.operators.observable.ObservableFromArray$FromArrayDisposable.run(ObservableFromArray.java:107)
at io.reactivex.internal.operators.observable.ObservableFromArray.subscribeActual(ObservableFromArray.java:36)
at io.reactivex.Observable.subscribe(Observable.java:11040)
at io.reactivex.internal.operators.observable.ObservableFlatMap.subscribeActual(ObservableFlatMap.java:55)
at io.reactivex.Observable.subscribe(Observable.java:11040)
at io.reactivex.internal.operators.observable.ObservablePublishSelector.subscribeActual(ObservablePublishSelector.java:59)
at io.reactivex.Observable.subscribe(Observable.java:11040)
at io.reactivex.internal.operators.observable.ObservableScanSeed.subscribeActual(ObservableScanSeed.java:47)
at io.reactivex.Observable.subscribe(Observable.java:11040)
at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96)
at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:463)
at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)
at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)