Does RxJava2 auto dispose observable when they call completed or error?

做~自己de王妃 提交于 2019-12-08 19:51:17

问题


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

When a sequence sends the completed or error event all internal resources that compute sequence elements will be freed.

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

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

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


回答1:


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);
}


来源:https://stackoverflow.com/questions/45589930/does-rxjava2-auto-dispose-observable-when-they-call-completed-or-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!