dispose vs clear CompositeDisposable RxJava

Authors
dispose vs clear CompositeDisposable RxJava

In RxJava, the clear() method of CompositeDisposable clears all the disposables but can accept a new one, whereas the dispose() method clears all the disposables and does not accept any new one.

In this blog, we will learn about the dispose method and the clear method of CompositeDisposable that is present in the RxJava.

I am Amit Shekhar, Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.

I teach AI and Machine Learning, and Android at Outcome School.

Join Outcome School and get a high-paying tech job:

Let's get started.

I have realized that many developers are having confusion about when to use the dispose() method and when to use the clear() method of CompositeDisposable. So, I will clarify it for you.

For that, let's take an example.

private val disposables = CompositeDisposable()

private fun sampleObservable(): Observable<String> {
    return Observable.defer {
        // Do some long running operation
        SystemClock.sleep(2000)
        return@defer Observable.just("one", "two", "three", "four", "five")
    }
}

// adding an Observable to the disposable
disposables.add(
    sampleObservable()
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribeWith(object : DisposableObserver<String>() {

            override fun onNext(value: String) {

            }

            override fun onError(e: Throwable) {

            }

            override fun onComplete() {

            }
        })
)

A quick note for you

No matter which tech domain you work in, get familiar with these topics:

  • LLM
  • RAG
  • MCP
  • Agent
  • Fine-tuning
  • Quantization

We put it all together in one video:

AI Engineering Explained: LLM, RAG, MCP, Agent, Fine-Tuning, and Quantization

No need to stop reading - bookmark it and watch later when you get time. Future you will thank you.

Now, let's get back to the topic.

Now, we have two methods available with the CompositeDisposable:

  • clear() - When we use the clear() method, it will clear all the disposables but can accept a new disposable.
disposables.clear()
  • dispose() - When we use the dispose() method, it will clear all the disposables and set isDisposed = true, so it will not accept any new disposable.
disposables.dispose()

This must have given you clarity. Now, you can decide which one to use when based on your requirement.

This was all about the dispose method and the clear method of CompositeDisposable that is present in the RxJava.

Stay updated: Subscribe to our newsletter to get our latest AI and Machine Learning blogs straight to your inbox.

Frequently Asked Questions

Can we add a new disposable after calling dispose() on a CompositeDisposable?

No. When we call dispose(), it clears all the disposables and sets isDisposed = true. After that, the CompositeDisposable will not accept any new disposable. So we should call dispose() only when we do not want to add anything to it again.

Can we reuse a CompositeDisposable after calling clear()?

Yes. When we call clear(), it clears all the disposables but can still accept a new disposable. So we can keep using the same CompositeDisposable and add new disposables to it later.

Do both clear() and dispose() remove the existing disposables?

Yes. Both methods clear all the disposables that were added. The only difference is what happens next. After clear(), the CompositeDisposable can accept new disposables, while after dispose(), it sets isDisposed = true and rejects any new disposable.

That's it for now.

Thanks

Amit Shekhar
Founder @ Outcome School

You can connect with me on:

Follow Outcome School on:

Read all of our high-quality blogs here.