現(xiàn)在MVP模式越來(lái)越流行了器予,使用起來(lái)很方便夺衍,這樣的話我們會(huì)更加的專(zhuān)注于業(yè)務(wù)。現(xiàn)在很多人也開(kāi)始愛(ài)上了RxJava了蹂风,但是在使用RxJava時(shí)我們要注意的是防止內(nèi)存泄露吗伤,因?yàn)楫?dāng)Activity已經(jīng)finish的時(shí)候如果這是訂閱關(guān)系還沒(méi)有取消,那么這個(gè)時(shí)候就會(huì)發(fā)生內(nèi)存泄漏硫眨,更嚴(yán)重的是,這個(gè)時(shí)候如果我們還繼續(xù)更新界面,那么程序就可能報(bào)空指針礁阁,因?yàn)檫@是相應(yīng)的UI元素已經(jīng)銷(xiāo)毀了巧号,所以這點(diǎn)我們是需要注意的,那怎么才能夠安全的使用RxJava了姥闭,這個(gè)時(shí)候RxLifeCycle就派上用場(chǎng)了丹鸿,原理其實(shí)很簡(jiǎn)單我們只需要綁定Activity或者Fragment的生命周期就可以了。下面我們談?wù)勗贛VP模式下棚品,如何使用RxLifeCycle靠欢。
首先我們需要添加對(duì)應(yīng)的依賴(lài),如下:
/**
* rxLifecycle
*/
implementation 'com.trello.rxlifecycle2:rxlifecycle-android:2.0.1'
implementation 'com.trello.rxlifecycle2:rxlifecycle-components:2.0.1'
這里我們要用到LifecycleProvide這個(gè)接口铜跑。值得一提的是這個(gè)接口已經(jīng)在RxAppCompatActivity中實(shí)現(xiàn)了门怪,所以我們BaseActivity可以很方便的繼承RxAppCompatActivity,賦予接口引用就可以使用它了锅纺。
我的做法是在BasePresenter中去構(gòu)建接口對(duì)象:
public abstract class BasePresenter<T> {
/**
* 這里可以初始化View接口
*/
public T view;
private LifecycleProvider<ActivityEvent>provider;
/**
*
* @param view Activity或者Fragment中的引用this
*/
public void attach(T view){
this.view=view;
}
public void detach(){
this.view=null;
}
public BasePresenter(LifecycleProvider<ActivityEvent>provider){
this.provider=provider;
}
public LifecycleProvider<ActivityEvent> getProvider() {
return provider;
}
public void setProvider(LifecycleProvider<ActivityEvent> provider) {
this.provider = provider;
}
}
業(yè)務(wù)層的使用如下:
public class NetWorkPresenter extends BasePresenter<NetWorkView>{
public NetWorkPresenter(LifecycleProvider<ActivityEvent> provider) {
super(provider);
}
public void getData(PageParmForm pageParmForm, Context mContext){
new NetRepository().getApplication(pageParmForm)
.compose(getProvider().<UserGuideSoftConfigRForm<UserGuideSoftConfigPageInfo<List<UserguideSoftConfig>>>>bindUntilEvent(ActivityEvent.DESTROY))
.subscribe(new BaseObserver<UserGuideSoftConfigRForm<UserGuideSoftConfigPageInfo<List<UserguideSoftConfig>>>>(mContext) {
@Override
public void onError(ExceptionHandle.ResponseException exception) {
view.toast(exception.getMessage());
}
@Override
public void onNext(UserGuideSoftConfigRForm<UserGuideSoftConfigPageInfo<List<UserguideSoftConfig>>> Response) {
view.showApplicationList(Response);
Log.d("lifecycle","pause");
}
@Override
public void onSubscribe(Disposable d) {
}
});
}
}
所以我們?cè)贏ctivity中實(shí)例化中直接傳this就可以了
@Override
public NetWorkPresenter initPresenter() {
return new NetWorkPresenter(this);
}
在Activity中的使用如下:
public void initData(){
new NetRepository().getApplication(new PageParmForm(Constant.orderStr,Constant.pageNum,Constant.pageSize))
.compose(this.<UserGuideSoftConfigRForm<UserGuideSoftConfigPageInfo<List<UserguideSoftConfig>>>>bindUntilEvent(ActivityEvent.PAUSE))//指定訂閱再PAUSE的時(shí)候呢掷空,取消訂閱
.subscribe(new BaseObserver<UserGuideSoftConfigRForm<UserGuideSoftConfigPageInfo<List<UserguideSoftConfig>>>>(NetWorkActivity.this) {
@Override
public void onError(ExceptionHandle.ResponseException exception) {
myToast.showToast(NetWorkActivity.this,exception.getMessage());
}
@Override
public void onNext(UserGuideSoftConfigRForm<UserGuideSoftConfigPageInfo<List<UserguideSoftConfig>>> Response) {
data=Response.getData().getList();
code=Response.getCode();
generateAdapter.setData(data);
generateAdapter.notifyDataSetChanged();
}
});
}
這樣使用起來(lái)就很簡(jiǎn)單
順便貼一下mvpActivity:
public abstract class MvpActivity<V,P extends BasePresenter<V>> extends BaseActivity {
protected P presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
presenter=initPresenter();//實(shí)例化Presenter
presenter.attach((V) this);//實(shí)例化接口引用
}
@Override
protected void onResume() {
super.onResume();
presenter.attach((V) this);//綁定view接口,相當(dāng)于拿到接口的引用
}
@Override
protected void onDestroy() {
super.onDestroy();
presenter.detach();//耗時(shí)操作在這里也結(jié)束
}
//實(shí)例化Presenter
public abstract P initPresenter();
}