joaquim-verges/Helium

[Question] How does BasePresenter not leak a Context?

Closed this issue · 2 comments

Hello!

I just saw your Droidcon 2018 talk and loved it. I hope you don't mind me asking some questions via this forum.

BasePresenter is passed a ViewDelegate class in attach which contains a Context as well as a Lifecycle. How does the presenter not leak the (Activity) Context by holding on to a reference to the delegate?

Similarly, the Android ViewModel overview says the following:

Caution: A ViewModel must never reference a view, Lifecycle, or any class that may hold a reference to the activity context.

Please correct me if I'm wrong, but I believe this contract is being violated by Helium in BasePresenter.

Thanks!

Hi @Plastix !

It's a good question, you should definitely never hold a ViewDelegate or a Context inside a presenter. The attach method in BasePresenter does not keep a reference on either (it doesn't keep it as fields in the class, and you should definitely never do that), however, it does subscribe to the relevant observers, which could leak the view delegate if those subscriptions are not disposed.

But thanks to the awesome AutoDispose library, it automatically disposes those subscriptions on the relevant lifecycle events. So if you call attach in onCreate, it will automatically dispose in onDestroy, similarly, if you attach in onResume for some reason, it will automatically dispose in onPause. That way, you don't have to worry about leaks! It's a really useful library.

Let me know if that answers your question.

Thanks @joaquim-verges. This answers my question(s) perfectly.