我正在尝试创建一个Observable
,以便它会在一定时间间隔内从网络加载一些数据,并且每当用户刷新页面时。这是我到目前为止的要点:
PublishSubject<Long> refreshSubject = PublishSubject.create();
Observable<MyDataType> observable = Observable.merge(
Observable.interval(0, 3, TimeUnit.SECONDS),
refreshSubject
)
.flatMap(t -> {
// network operations that eventually return a value
// these operations are not observables themselves
// they are fully blocking network operations
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(data -> {
// update ui with data
}, error -> {
// do something with error
});
稍后在刷新回调中我有:
refreshSubject.onNext(0L);
它在间隔很好的情况下运行,但是当我刷新时,它会以NetworkOnMainThreadException
爆炸。我以为我是通过subscribeOn
/ observeOn
处理此问题的。我错过了什么?另外,为什么在从间隔触发Observer
时这不会导致崩溃?
答案 0 :(得分:5)
您必须将subscribeOn(Schedulers.io())
更改为observeOn(Schedulers.io())
并将其移到flatMap上。
原因是您的refreshSubject
是一个PublishSubject,它是一个Observable和一个Observer。
由于在结果传递到您的订阅之前,首先在实习生Observable中调用此PublishSubject的onNext()
。
这也是当你使用Observable时它工作的原因(以及interval
默认情况下总是订阅计算线程的事实。)
只需检查这两个片段的输出:
Observable.merge(
Observable.interval(0, 3, TimeUnit.SECONDS),
refreshSubject
)
.observeOn(Schedulers.io())
.doOnNext(aLong -> Log.d("Thread", Thread.currentThread().toString()))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(data -> {
Log.d("Subscribe Thread", Thread.currentThread().toString());
}, error -> {
// do something with error
});
VS
Observable.merge(
Observable.interval(0, 3, TimeUnit.SECONDS),
refreshSubject
)
.doOnNext(aLong -> Log.d("Thread", Thread.currentThread().toString()))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(data -> {
Log.d("Subscribe Thread", Thread.currentThread().toString());
}, error -> {
// do something with error
});