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中無時無刻都要注意的地方。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章