【问题标题】:RxJava global handler and Android VitalsRxJava 全局处理程序和 Android Vitals
【发布时间】:2019-08-20 07:34:33
【问题描述】:

我对 RxJava 全局处理程序和 Android Vitals 有疑问。 在 Android Vitals 中我可以看到

io.reactivex.exceptions.UndeliverableException

引起:java.lang.InterruptedException:在 com.google.common.util.concurrent.AbstractFuture.get (AbstractFuture.java:527) 在 com.google.common.util.concurrent.FluentFuture$TrustedFuture.get (FluentFuture.java:82)

但是我已经不知道问题出在哪里,所以我考虑添加 RxJava 全局错误处理程序:

RxJavaPlugins.setErrorHandler(e -> {
    if (e instanceof UndeliverableException) {
        e = e.getCause();
    }
    if ((e instanceof IOException) || (e instanceof SocketException)) {
        // fine, irrelevant network problem or API that throws on cancellation
        return;
    }
    if (e instanceof InterruptedException) {
        // fine, some blocking code was interrupted by a dispose call
        return;
    }
    if ((e instanceof NullPointerException) || (e instanceof IllegalArgumentException)) {
        // that's likely a bug in the application
        Thread.currentThread().getUncaughtExceptionHandler()
            .handleException(Thread.currentThread(), e);
        return;
    }
    if (e instanceof IllegalStateException) {
        // that's a bug in RxJava or in a custom operator
        Thread.currentThread().getUncaughtExceptionHandler()
            .handleException(Thread.currentThread(), e);
        return;
    }
    Log.warning("Undeliverable exception received, not sure what to do", e);
});

还有我的问题。如果我要添加全局错误处理程序,我会丢失来自 Android Vitals 的报告吗?丢失的意思是不会有新的报告,因为我们将处理导致崩溃的错误。

是否可以添加全局错误处理程序并仍然在 Android Vitals 中获得报告?

【问题讨论】:

    标签: java android kotlin rx-java2 android-vitals


    【解决方案1】:

    您的目标应该是减少崩溃。因此,只要您能够正确处理异常,就应该这样做。

    UndeliverableException 通常会在无法将异常传递给观察者时弹出。当Subject 没有观察者时,可能会出现这种情况。

    通常您可以轻松解决这些问题。或者您可以忽略它并重新抛出任何其他错误。

    RxJavaPlugins.setErrorHandler(e -> {
        if (e instanceof UndeliverableException) {
            return;
        }
        throw e;
    });
    

    也许还记录问题以了解它。

    【讨论】:

    • 可以肯定。全局错误处理程序只会在观察者不能处理错误时处理?否则正常的 onError() 会被调用?
    • 如果你的本地 onError 有效,它会一直被调用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-06
    • 2012-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-05
    相关资源
    最近更新 更多