ฉันกำลังพยายามสร้าง 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
ถูกทริกเกอร์จากช่วงเวลา