Where to clear CompositeDisposable in RecyclerView.Adapter

1k Views Asked by At

Consider we have some Disposable in our RecyclerView.Adapter and we added them to a CompositeDisposable.
which Adapter method callback is the best choice to clear() the CompositeDisposable ?

Currently I did on the onDetachedFromRecyclerView. I want to be sure about how correct is this.

1

There are 1 best solutions below

0
On BEST ANSWER

It would be easier to answer if you could provide the code of the adapter. In general, Disposable should be disposed with respect to your business logic and the component containing lifecycle.

I would also say that it is better to not use Rx inside of RecyclerView adapter. Here are the benefits:

  • Adapter logic remains simple and synchronous.
  • You don't need to think about possible lifecycle or multithreading issues when developing the adapter.
  • Rx streams always allocate a lot of memory, so (if we talk about RecyclerView) putting them into the wrong place can lead to performance issues.

So, I'd suggest moving Rx streams into Presenter/ViewModel or similar component.