Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2019-09-03:MVP中你是如何处理Presenter层以防止内存泄漏的? #139

Open
Moosphan opened this issue Sep 3, 2019 · 17 comments

Comments

@Moosphan
Copy link
Owner

Moosphan commented Sep 3, 2019

No description provided.

@TzuChiangLi
Copy link

我常用的防止该问题的方法是直接换MVC

@lvasd
Copy link

lvasd commented Sep 3, 2019

我常用的防止该问题的方法是直接换MVC

还是换mvvm吧

@424385117
Copy link

没用过mvp,好像是Presenter持有activity的引用,activity是lifecycle的子类,也是通过lifecycle来释放资源

@424385117
Copy link

或者直接换mvvm

@leftcoding
Copy link

leftcoding commented Sep 3, 2019

在 Activity 执行 onDestroy() 方法之前,调用 Presenter 自定义一个 onDestroy() 方法,因为 Presenter 持有 Activity View 的应用,所以在 Presenter 的 onDestroy() 方法里面把 View = null,在 Presenter 执行 网络请求或者其他的耗时操作,结果返回都必须执行 if(view != null )操作判断。

@TzuChiangLi
Copy link

TzuChiangLi commented Sep 3, 2019 via email

@yizems
Copy link

yizems commented Sep 3, 2019

1 ondestory 手动 view=null,回调的时候 view?.xxx
2. 利用lifecycle,记不太清了,自行百度吧
3. 换mvvm

这是目前最常用的方式吧, 但是 MVC 是大坑吧,不能解决 内存泄漏,反而更加严重才对

@TzuChiangLi
Copy link

TzuChiangLi commented Sep 3, 2019 via email

@0xddy
Copy link

0xddy commented Sep 3, 2019

我选择 mmp ,MMP yes !!

@yizems
Copy link

yizems commented Sep 3, 2019

@dingyong666 对得起你的名字,如果是我想的那个意思的话=.=

@war911
Copy link

war911 commented Sep 3, 2019

使用WeakReference 持有V的弱引用,在gc回收扫描到弱引用的时候,就会它回收掉。

Activity onCreate中注册,onStop 解注册。

@fogcoding
Copy link

使用WeakReference 持有V的弱引用,在gc回收扫描到弱引用的时候,就会它回收掉。

Activity onCreate中注册,onStop 解注册。

onStop里面取消注册怕是不行吧,切换Acitivity的时候会调用onStop但是换回来的时候并不会调用onCreate.那样岂不是回到原来的Activity没有presenter了?

@shortybin
Copy link

首先 MVP 会出现内存泄漏是因为 Presenter 层持有 View 对象,一般我们会把 Activity 做为 View 传递到 Presenter,Presenter 持有 View对象,Activity 退出了但是没有回收出现内存泄漏。

解决办法:
1.Activity onDestroy() 方法中调用 Presenter 中的方法,把 View 置为 null
2.使用 Lifecycle
3.使用 MVVM

@war911
Copy link

war911 commented Sep 4, 2019

使用WeakReference 持有V的弱引用,在gc回收扫描到弱引用的时候,就会它回收掉。
Activity onCreate中注册,onStop 解注册。

onStop里面取消注册怕是不行吧,切换Acitivity的时候会调用onStop但是换回来的时候并不会调用onCreate.那样岂不是回到原来的Activity没有presenter了?

那就在onDestroy 解注册。

@small-tree
Copy link

没用过mvp,好像是Presenter持有activity的引用,activity是lifecycle的子类,也是通过lifecycle来释放资源

Presenter 持有 activity 是lifecycle的子类,通过注册 lifecyclerObserver 监听 distory 方法,取消异步任务。

@LillteZheng
Copy link

一般使用mvp,都有一个 BaseActivity 或者一个 BaseFragment,而 P 层也有个 BasePresent,我们可在 BasePresent 设置两个方法,onAttach(View view) onDeathView(View view); 在 BaseActivit 的onCreate或 BaseFragment 的onCreateView 使用 onAttach,而在 onDestroy 或者 onDestroyView 使用 onDeathView 解绑;同理,如果 p 层也有使用网络,也可以使用这个方法;

@jingerlovexiaojie
Copy link

jingerlovexiaojie commented Apr 1, 2021

attachView():在Activity/Fragment执行生命周期方法onCreate()的时候

detachView():Activity/Fragment执行执行生命周期方法onDestroy()的时候
`

public class GoodsPresenter {

//持有View层的引用
public WeakReference<T> iGoodsView;

//持有Model层的引用
IGoodsModel iGoodsModel = new GoodsModel();

public void attachView(T view) {
    iGoodsView = new WeakReference<>(view);
}

public void detachView() {
    if (iGoodsView != null) {
        iGoodsView.clear();
        iGoodsView = null;
    }

}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests