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

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