1. 程式人生 > >Android中rxJava 2.x的observeOn報錯NoClassdefound

Android中rxJava 2.x的observeOn報錯NoClassdefound

rxJava 2.x Observable回撥observeOn報錯的問題

最近從rxJava 1.x轉到新版rxJava 2.x時,在使用Observable回撥observeOn(AndroidSchedulers.mainThread())時,報了以下錯誤

java.lang.NoClassDefFoundError: io.reactivex.Flowable

只知道rxJava 2.x對程式碼進行了重構,加入了背壓的概念,然後這邊查閱了一下Observable的api。

observeOn(Scheduler scheduler)
Modifies an ObservableSource to perform its emissions and notifications on a specified Scheduler, asynchronously with an unbounded buffer with Flowable

.bufferSize() “island size”.

observeOn(Scheduler scheduler, boolean delayError)
Modifies an ObservableSource to perform its emissions and notifications on a specified Scheduler, asynchronously with an unbounded buffer with Flowable.bufferSize() “island size” and optionally delays onError notifications.

observeOn(Scheduler scheduler, boolean delayError, int bufferSize)
Modifies an ObservableSource to perform its emissions and notifications on a specified Scheduler, asynchronously with an unbounded buffer of configurable “island size” and optionally delays onError notifications.

發現observeOn可以匯入一個或兩個或三個引數,且匯入一個和兩個引數的方法有個with Flowable

字樣,表示只用與Flowable,匯入三個引數的方法沒有其字樣,然後我用第三種方法observeOn(AndroidSchedulers.mainThread(),false,100),執行成功。

“island size”

這三個方法都提及到了”island size”,中文字面翻譯“島嶼大小”,比較抽象,我把它叫做背壓堆積大小。

rxJava 2.x中只有兩個觀察者,一個是Observable,一個是Flowable。它合併了rxJava 1.x的Subscriber,並多了一個onSubscribe方法(即原來Subscriber的onStart方法),用來進行訂閱的初始化操作。

Flowable自動背壓堆積並支援匹配背壓堆積大小”island size”,Observable並不會,所以在Observable訂閱時要多加入一個int值,用與指定背壓堆積大小。

所以,背壓概念是rxJava 2.x中無時無刻都要注意的地方。