致命异常:触发器配置时RxCachedThreadScheduler-1。为什么?

时间:2017-07-10 14:45:56

标签: java rx-java kotlin rx-java2

我有以下RxJava 2代码(在Kotlin中),它有一个Observable

disposable = Observable.create<String>({
    subscriber ->
            try {
                Thread.sleep(2000)
                subscriber.onNext("Test")
                subscriber.onComplete()
            } catch (exception: Exception) {
                subscriber.onError(exception)
            }
}).subscribeOn(Schedulers.io())
  .observeOn(AndroidSchedulers.mainThread())
  .subscribe({ result -> Log.d("Test", "Completed $result") },
             { error -> Log.e("Test", "Completed ${error.message}") })

虽然它仍为Thread.sleep(2000),但我会执行disposable?.dispose()来电,会出错

FATAL EXCEPTION: RxCachedThreadScheduler-1
Process: com.elyeproj.rxstate, PID: 10202
java.lang.InterruptedException 
    at java.lang.Thread.sleep(Native Method)
    at java.lang.Thread.sleep(Thread.java:371)
    at java.lang.Thread.sleep(Thread.java:313)
    at presenter.MainPresenter$loadData$1.subscribe(MainPresenter.kt:41)
    at io.reactivex.internal.operators.observable.ObservableCreate.subscribeActual(ObservableCreate.java:40)

我希望dispose有助于以静默方式取消操作,或者最多会在订阅时使用Log.e捕获错误。但是,它只是按照上面的错误消息崩溃。

为什么Exception会逃脱?是不是dispose假设在没有崩溃的情况下以静默方式取消整个操作?

2 个答案:

答案 0 :(得分:3)

There is a combination of factors here:

  1. dispose of a stream that uses subscribeOn also disposes of the thread used. This also involves calling Thread.interrupt() when using Schedulers.io(). This causes the exception in your case.
  2. InterruptedException is an Exception thrown by Thread.sleep, so it is caught by your code and passed to onError like any other exception.
  3. Calling onError after dispose redirects the error to the global error handler due to RxJava2's policy of NEVER throwing away errors. To work around this check subscriber.isDisposed() before calling onError or use RxJava 2.1.1's new subscriber.tryOnError.

    if (!subscriber.isDisposed()) {
      subscriber.onError(exception)
    }
    

答案 1 :(得分:0)

如果您使用的是rxjava2,请将其添加到初始化代码中

driver.find_element_by_xpath("(//button[@class='expedition_button awesome-button '])[1]")

希望有帮助