Java 取消订阅时清除资源

Java 取消订阅时清除资源,java,android,rx-java,Java,Android,Rx Java,当订阅已取消订阅时,我在执行某些逻辑时遇到一些问题。我已经做了几个小时了,到目前为止,我几乎没有取得什么进展。这是我的代码的简化版本: public class Command<E> { public CommandActionObservable execute() { final CommandAction<E> command = createCommand(); final OnSubscribe<CommandA

当订阅已取消订阅时,我在执行某些逻辑时遇到一些问题。我已经做了几个小时了,到目前为止,我几乎没有取得什么进展。这是我的代码的简化版本:

public class Command<E> {

    public CommandActionObservable execute() {
        final CommandAction<E> command = createCommand();

        final OnSubscribe<CommandAction<E>> onSubscribe = (subscriber) -> {

            /* Create a listener that handles notifications and register it.
             * The idea here is to push the command downstream so it can be re-executed
             */
            final Listener listener = (event) -> {
              subscriber.onNext(command);
            }
            registerListener(listener);

            /* This is where I'm having trouble. The unregister method
             * should be executed when the subscriber unsubscribed, 
             * but it never happens
             */
            subscriber.add(Subscriptions.create(() -> {
                unregisterListener(listener);
            }));

            // pass the initial command downstream
            subscriber.onNext(command);

            kickOffBackgroundAction();             
        }

        final Observable<CommandAction<E>> actionObservable = Observable.create(onSubscribe)
            .onBackpressureLatest()
            .observeOn(Shedulers.io())
            .onBackpressureLatest();
        return new CommandActionObservable((subscriber) -> {
            actionObservable.unsafeSubscribe(subscriber);
        })
    }

    public class CommandActionObservable extends Observable<CommandAction<E> {

        // default constructor omitted

        public Observable<E> toResult() {
            return lift((Operator) (subscriber) ->  {
                return new Subscriber<CommandAction<E>>() {
                    // delegate onCompleted and onError to subscriber

                    public void onNext(CommandAction<E> action) {
                        // execute the action and pass the result downstream
                        final E result = action.execute();
                        subscriber.onNext(result)
                    }
                } 
            }
        }

    }

}
如前所述,我无法让取消订阅逻辑工作并取消注册侦听器,这会导致应用程序内存泄漏。如果我在
obs
上调用
dounsubscribe()
,它会被调用,因此我是正确地取消订阅的,但可能观察到的嵌套和提升会导致一些问题


我很乐意就这一点发表意见。

事实证明,这比我预期的要容易得多

经过一番摸索,我终于能够自己想出答案。只是为那些可能会和我处于同样处境的人发布这篇文章

因此,正如我在问题中提到的,如果我在我的
活动中获得的可观察到的内容中添加了
doOnSubscribe()
操作,它会得到通知。接下来,我尝试在我在
execute()
方法中创建的内部
可观察对象上添加相同的操作。他们没有接到电话。因此,我得出结论,在我的活动中的可观察对象和我在
execute()
中创建的可观察对象之间的某个地方,链条正在断裂

流中唯一发生的事情是在
toResult()
中实现的自定义
操作符的应用程序。在谷歌搜索之后,我发现了这篇优秀的文章-。我确实在我的操作员中制动链条,上游观察者没有收到取消预订的通知

在我按照作者的建议做了之后,一切都很好。以下是我需要做的:

lift((Operator) (subscriber) -> {
    // connect the upstream and downstream subscribers to keep the chain intact
    new Subscriber<CommandAction<E>>(subscriber) {
        // the implementation is the same
    }
}
lift((操作员)(订户)->{
//连接上游和下游用户以保持链的完整性
新订户(订户){
//实现是相同的
}
}
lift((Operator) (subscriber) -> {
    // connect the upstream and downstream subscribers to keep the chain intact
    new Subscriber<CommandAction<E>>(subscriber) {
        // the implementation is the same
    }
}