RxJava1升级为RxJava2

升级的原因:app在后台运行过久或者一会,重新打开时,有几率闪退。

抓到的log:
java.lang.IllegalStateException: Exception thrown on Scheduler.Worker thread. Add onError handling.
Caused by: rx.exceptions.MissingBackpressureException

java.lang.IllegalStateException: Exception thrown on Scheduler.Worker thread. Add `onError` handling.
    at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:57)
    at android.os.Handler.handleCallback(Handler.java:891)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:207)
    at android.app.ActivityThread.main(ActivityThread.java:7470)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)
 Caused by: rx.exceptions.OnErrorNotImplementedException
    at rx.internal.util.InternalObservableUtils$ErrorNotImplementedAction.call(InternalObservableUtils.java:386)
    at rx.internal.util.InternalObservableUtils$ErrorNotImplementedAction.call(InternalObservableUtils.java:383)
    at rx.internal.util.ActionSubscriber.onError(ActionSubscriber.java:44)
    at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:153)
    at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:115)
    at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.checkTerminated(OperatorObserveOn.java:273)
    at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:216)
    at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
    at android.os.Handler.handleCallback(Handler.java:891) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:207) 
    at android.app.ActivityThread.main(ActivityThread.java:7470) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958) 
 Caused by: rx.exceptions.MissingBackpressureException
    at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.onNext(OperatorObserveOn.java:160)
    at rx.internal.operators.OperatorSubscribeOn$1$1.onNext(OperatorSubscribeOn.java:53)
    at rx.internal.operators.OnSubscribeTimerPeriodically$1.call(OnSubscribeTimerPeriodically.java:52)
    at rx.Scheduler$Worker$1.call(Scheduler.java:137)
    at rx.internal.schedulers.EventLoopsScheduler$EventLoopWorker$2.call(EventLoopsScheduler.java:189)
    at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:784)

百度出来的解释是产生事件太快,且太多,处理事件的速度太慢。就好像家里面米太多,吃不完,放太久坏掉了。

RxJava1没有处理这种情况,也可以百度搜背压处理。如果能解决就不用更新了。我项目不行,只好大改。
听说RxJava2有对这种情况处理

换就完事了

升级步骤如下

步骤:1、

api 'com.trello:rxlifecycle-components:1.0'
api 'com.trello:rxlifecycle:1.0' 

改为

api 'com.trello.rxlifecycle2:rxlifecycle:2.2.1'
api 'com.trello.rxlifecycle2:rxlifecycle-android:2.2.1'
api 'com.trello.rxlifecycle2:rxlifecycle-components:2.2.1'

2、subscribe不再支持单个Subscriber了,
现支持需要3个参数

subscribe(Consumer super T> onNext, Consumer super Throwable> onError,
Action onComplete)
或者subscribe(Observer super T> observer)

项目中改动位置示例
.subscribe(new ProgressSubscriberNext(basePar, listenerSoftReference, views),new ProgressSubscriberError(listenerSoftReference, views),new ProgressSubscriberCompleted(listenerSoftReference, views));
3、不支持Subscription来解除订阅了,用Disposable来代替了
项目中改动位置示例

fun dealCountDownS(second:Int,disposableObserver: Consumer): Disposable {
return Observable.interval(1, TimeUnit.SECONDS, AndroidSchedulers.mainThread())
.take(second as Long)
.subscribe(disposableObserver)
}

有问题请留言

发表评论

您的邮箱地址不会被公开。 必填项已用 * 标注