Reputation: 3080
I have a MVP Fragment (View) that hosts a custom implementation of a RecyclerView adapter (RecyclerViewAdapter). This adapter extends my custom RecyclerViewAdapterBase adapter. As the Presenter hosting the Fragment needs to know what data is currently modelled, the data which the RecyclerView will utilize is stored as a private field in the Presenter.
This being said, I am currently passing the Presenter to the RecyclerViewAdapter through the Adapter's constructor. Is this a risk?
The only risk I can see is the RecyclerViewAdapter outliving the Fragment and thus outliving the Presenter. However, this is not a possibility as the Fragment holds a private reference to the Adapter and the Presenter and both would be destroyed at the same time.
This being said, I've thought of only passing the data required to the RecyclerViewAdapter in the constructor, but then I am not certain how to update the data without a reference to the Presenter? (Where the data is currently modelled.)
Thank you for your ideas!
Upvotes: 4
Views: 1452
Reputation: 4573
Presenter hosting the Fragment needs to know what data is currently modelled
You just pass the data that you want to present, don't store it inside a presenter.
I am currently passing the Presenter to the RecyclerViewAdapter through the Adapter's constructor. Is this a risk?
No. Adapter is just an object, it does not have its own lifecycle like Activity
or Thread
does. If activity gets destroyed -> adapter gets destroyed. If adapter gets destroyed -> presenter gets destroyed. No leaks here, you are good.
This being said, I've thought of only passing the data required to the RecyclerViewAdapter in the constructor, but then I am not certain how to update the data without a reference to the Presenter?
Depends on how frequently you are going to use your presenter. If you only need to draw it once, then you can create a presenter, use it once and thus avoid the reference to the presenter. But, you have an adapter which represents many views, so, I'm pretty sure you are going to draw a lot. So keep a reference to the presenter from your adapter, this is absolutely fine. There are no memory and performance concerns here.
Upvotes: 2