androidandroid-recyclerviewrx-javarx-java2disposable

Where to clear CompositeDisposable in RecyclerView.Adapter


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.


Solution

  • 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:

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