在Android上调试时,RxJava缓存线程中的InterruptedException

Avs*_*tor 7 android interrupted-exception rx-java rx-android

有时当我调试我的应用程序时,我在RxCachedThreadScheduler-1中遇到InterruptedException.这是跟踪:

Fatal Exception: java.lang.InterruptedException
       at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.reportInterruptAfterWait(AbstractQueuedSynchronizer.java:1991)
       at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2025)
       at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1048)
       at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:776)
       at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1035)
Run Code Online (Sandbox Code Playgroud)

我有一个自定义视图,我在其中订阅我的observable,如下所示:

@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();

    sub = FollowHandler.getInstance().getObservable()
            .filter(new Func1<FollowEvent, Boolean>() {
                @Override
                public Boolean call(FollowEvent followEvent) {
                    if(followEvent == null || followEvent.user == null
                            || user == null)
                        return false;

                    return followEvent.user.id == user.id;
                }
            })
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<FollowEvent>() {
                @Override
                public void onCompleted() {}

                @Override
                public void onError(Throwable e) {}

                @Override
                public void onNext(FollowEvent followEvent) {
                    reactToThisNiceEvent(followEvent);
                }
            });
}

@Override
protected void onDetachedFromWindow() {
    super.onDetachedFromWindow();

    if(sub != null)
        sub.unsubscribe();
}
Run Code Online (Sandbox Code Playgroud)

这是可观察的:

eventSubject.asObservable()
        .observeOn(Schedulers.io())
        .doOnNext(new Action1<FollowEvent>() {
            @Override
            public void call(FollowEvent followEvent) {
                if(followEvent != null)
                    doSomethingNice(followEvent);
            }
        })
        .share();
Run Code Online (Sandbox Code Playgroud)

其中eventSubject是一个简单的PublishSubject.我使用RxAndroid 1.1.0和RxJava 1.1.0.

有谁知道为什么会这样?

Ale*_*der 0

我不确定为什么会发生,但尝试这样做:

sub = FollowHandler.getInstance().getObservable()
            .filter(...)
            .subscribeOn(Schedulers.io())    //  <<<<<<<<<<
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(...);
Run Code Online (Sandbox Code Playgroud)

另外,我认为你不需要share()

eventSubject.asObservable()
        .doOnNext(...)
        .subscribeOn(Schedulers.io())   // <<<<< subscribeOn instead of observeOn, but actually, you don't need it here...
        .share();     // <<<<< remove it
Run Code Online (Sandbox Code Playgroud)

Subject正如我上面所描述的,我经常使用事件总线。我从来没有遇到过这样的问题。

PS InonDetachedFromWindow()如果你能检查订阅是否取消订阅会更好。我知道这个方法在主线程中调用,并且并发访问它Subscription是不可能的,但我认为这是很好的风格:

if(sub != null && !sub.isUnsubscribed())
        sub.unsubscribe();
Run Code Online (Sandbox Code Playgroud)