Search code examples
javaandroidsystem.reactivereactivex

Why do we need compositedisposable in Rx and what was used before in normal java?


I'm used to develop android application using java.

Now I'm learning Kotlin , Reactivex and MVVM. I came across a compositedisposable concept

as per my understanding it helps to cancel for example the API call if user left the activity.

How this situation were handled before in java? I never had the need to cancel an api call why is it a thing in rx?


Solution

  • This is not because of Kotlin or Rx. It is a thing in Java and any other language or framework.

    Assume you have a long running async operation. It does not matter if it's a Thread, RxJava or Coroutines or whatever async framework you are using. When the applications finishes, you should cancel this long running operation (if you do not need the result anymore) to free up resources like memory or CPU.

    In RxJava you can use a Disposable or CompositeDisposable (which is just a collection of Disposable's) to cancel these operations.