【问题标题】:AutoDispose: How to handle error from observable after life cycle expiresAutoDispose:如何在生命周期到期后处理来自 observable 的错误
【发布时间】:2018-07-11 21:32:20
【问题描述】:

我使用 RxJava 在后台运行 http 操作。我使用生命周期转换器来避免在活动完成时收到事件(最初是 Trellos RxLifeCycle,现在我正在尝试使用 AutoDispose)。我的问题是如果活动完成,然后后台作业抛出异常,然后我得到一个异常,导致应用程序从 RxJava 崩溃。

问题是:是否可以使用生命周期提供程序(AutoDispose),但仍然处理生产者抛出的异常,从而避免它被 RxJava 传播到主线程并杀死应用程序?

下面的代码等待活动停止,然后抛出异常。除非我删除 AutoDispose 行,否则 Android 应用程序会崩溃。

 Observable.create(e -> {
            // Wait for life cycle to see my activity is stopped, then throw simulation IOException    
            while (!stopped) {
                Log.d(TAG, "Sleeping");
                try { Thread.sleep(1000); } catch (InterruptedException ignore) {}
            }
            throw new IOException("No AutoDispose 1");
        }).subscribeOn(Schedulers.io())
               .observeOn(AndroidSchedulers.mainThread())
               .to(AutoDispose.with(AndroidLifecycleScopeProvider.from(activity, Lifecycle.Event.ON_STOP)).forObservable())
                .subscribe((it) -> {}, (throwable -> Log.w(TAG, "Caught error  " + throwable)));

这个问题与 AutoDispose 有关,因为由于Dan Lew 提到的问题,我打算继续前进。虽然我很好奇这个问题是否也可以通过 RxLifeCycle 来解决。

Here's a full activity for easier execution @ gist

【问题讨论】:

  • 我遇到了和你一样的问题。我也评论了 github 问题 (github.com/uber/AutoDispose/issues/164)。我能想到的唯一解决方法是调用tryOnError 而不是onError,但这显然只有在您控制可观察源时才有效。

标签: android error-handling rx-java autodispose


【解决方案1】:

聚会有点晚了,但最终在https://github.com/uber/AutoDispose/issues/164#issuecomment-393727626得到了答复

行为良好的源会在发出事件之前检查isDisposed()。在睡觉的情况下,真的是尽力而为。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-02
    • 2014-09-10
    • 1970-01-01
    • 2017-05-03
    • 2013-11-26
    相关资源
    最近更新 更多