Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 296
Watcharin.s Avatar asked Aug 09 '17 11:08

Watcharin.s


1 Answers

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);
}
like image 67
hgrey Avatar answered Nov 12 '22 00:11

hgrey