2

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.

alizeyn
  • 2,300
  • 1
  • 20
  • 30
  • 3
    well, should you be using rx in a recyclerview ? – a_local_nobody Aug 22 '19 at 12:58
  • It's an inbox (list of messages) and disposables are response of actions like `deleteMessage` or `readMessage`. What is wrong with it? @a_local_nobody – alizeyn Aug 22 '19 at 13:02
  • 2
    well you could implement a callback to move these actions out to something like a viewmodel, then just handle the disposables there like you normally would :D – a_local_nobody Aug 22 '19 at 13:03
  • 1
    I was blind but now I can see @a_local_nobody – alizeyn Aug 22 '19 at 13:05
  • 1
    hope this helps, i assume you know how to implement callbacks to handle this otherwise you can try rework some of the stuff i've done here : https://stackoverflow.com/questions/57330766/how-to-get-data-from-any-asynchronous-operation-in-android/57330767#57330767 – a_local_nobody Aug 22 '19 at 13:06

1 Answers1

4

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.

Vadim
  • 141
  • 4