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

前端 未结 1 1536
既然无缘
既然无缘 2021-01-11 16:19

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

相关标签:
1条回答
  • 2021-01-11 17:16

    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);
    }
    
    0 讨论(0)
提交回复
热议问题