本文介绍了当RxJava2调用complete或error时,它们是否会自动处置可观察的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对RxJava的处置有疑问.我在Github上的RxSwift文档中找到了下面的句子.

I have a question about the disposal on RxJava. I found this below sentence on RxSwift document on Github.

要立即取消产生序列元素和释放资源,请在返回的订阅上调用dispose.

To cancel production of sequence elements and free resources immediately, call dispose on the returned subscription.

如果我正确理解,资源(可观察对象)将在调用 onCompleted onError 后被释放.

if I understand correctly the resources (observables) will be freed after they call onCompleted or onError.

所以问题是,RxJava是否执行与RxSwift相同的操作,或者我需要自己调用该处置?

So the question is, does RxJava do the same thing like RxSwift or I need to call the dispose by myself?

推荐答案

是的,所有关联的资源将被自动处置.为了说明使用RxJava 2进行以下运行测试:

Yes, all associated resources will be disposed automatically. To illustrate run following test with RxJava 2:

boolean isDisposed = false;

@Test 
public void testDisposed(){
    TestObserver<Integer> to = Observable.<Integer>create(subscriber -> {
        subscriber.setDisposable(new Disposable() {

            @Override
            public boolean isDisposed() {
                return isDisposed;
            }

            @Override
            public void dispose() {
                isDisposed = true;
            }
        });
        subscriber.onComplete();
    }).test();

    to.assertComplete();
    assertTrue(isDisposed);
}

这篇关于当RxJava2调用complete或error时,它们是否会自动处置可观察的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 12:49