问题
在使用rxjava的时候,如果不及时解除订阅,当退出acitivy的时候,异步线程还在执行,对activity还存在引用,此时就会产生内存泄漏。
解决
使用第三方的rxlifecycle能够很方便的解决这个问题。他可以让Observable发布的事件和当前的组件绑定,实现生命周期同步。从而实现当前组件生命周期结束时,自动取消对Observable订阅。
参考blog
废话说了一大堆,本文主要介绍在mvp中使用rxlifecycle
添加依赖
//处理rxjava内存泄漏
compile 'com.trello.rxlifecycle2:rxlifecycle:2.0.1'
compile 'com.trello.rxlifecycle2:rxlifecycle-android:2.0.1'
compile 'com.trello.rxlifecycle2:rxlifecycle-components:2.0.1'
建立BaseActivity
将自己的BaseActivity继承RxAppCompatActivity,或者你自己构建一个RxActivity也可以,看一下RxAppCompatActivity源码即可。
public abstract class RxAppCompatActivity extends AppCompatActivity implements LifecycleProvider<ActivityEvent> {
private final BehaviorSubject<ActivityEvent> lifecycleSubject = BehaviorSubject.create();
@Override
@NonNull
@CheckResult
public final Observable<ActivityEvent> lifecycle() {
return lifecycleSubject.hide();
}
@Override
@NonNull
@CheckResult
public final <T> LifecycleTransformer<T> bindUntilEvent(@NonNull ActivityEvent event) {
return RxLifecycle.bindUntilEvent(lifecycleSubject, event);
}
@Override
@NonNull
@CheckResult
public final <T> LifecycleTransformer<T> bindToLifecycle() {
return RxLifecycleAndroid.bindActivity(lifecycleSubject);
}
@Override
@CallSuper
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lifecycleSubject.onNext(ActivityEvent.CREATE);
}
@Override
@CallSuper
protected void onStart() {
super.onStart();
lifecycleSubject.onNext(ActivityEvent.START);
}
@Override
@CallSuper
protected void onResume() {
super.onResume();
lifecycleSubject.onNext(ActivityEvent.RESUME);
}
@Override
@CallSuper
protected void onPause() {
lifecycleSubject.onNext(ActivityEvent.PAUSE);
super.onPause();
}
@Override
@CallSuper
protected void onStop() {
lifecycleSubject.onNext(ActivityEvent.STOP);
super.onStop();
}
@Override
@CallSuper
protected void onDestroy() {
lifecycleSubject.onNext(ActivityEvent.DESTROY);
super.onDestroy();
}}
接口
interface Iview{
LifecycleTransformer bindLifecycle();
}
Activity中实现Iview接口的bindLifecycle()方法
@Override
public LifecycleTransformer bindLifecycle() {
//RxAppCompatActivity中的方法bindToLifecycle()
LifecycleTransformer objectLifecycleTransformer = bindToLifecycle();
return objectLifecycleTransformer;
}
presenter中使用
关键代码.compose(iview.bindLifecycle())
用的mvp模式,网络请求用的rxjava加retrofite。mvp和网络框架这里就不介绍了,网上有很多。
其中iview是activity传过来的接口,这里或者是activity的弱引用对象等等,反正就是用来调用activity中的方法,联系activity与presenter的。换成你自己的就行了。
参考blog
RxRetrofitFactory.create(MyService.class).getRecords("自己网络请求的参数")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.compose(iview.bindLifecycle())
.subscribe(new Observer<Message<List<ResultBean>>>() {
@Override
public void onSubscribe(Disposable d) {
Log.e("onSubscribe","onSubscribe--------------------------------------");
//d.dispose();//解除订阅(在这里解除订阅,后面的都不会执行了)
}
@Override
public void onNext(Message<List<ResultBean>> records) {
Log.e("onNext","onNext--------------------------------------");
List<ResultBean> recordList=records.Result;
iview.addRecordToList(recordList);
}
@Override
public void onError(Throwable e) {
Log.e("onError","onError--------------------------------------");
iview.showLoading(false);
}
@Override
public void onComplete() {
Log.e("onComplete","onComplete,--------------------------------------");
iview.showLoading(false);
}
});
`
后记
默认bindToLifecycle会在onPause方法执行前解除绑定,这样,在我们请求还未得到结果时,我们进入下一个页面,这个请求就会中断。为了避免这种情况,请使用compose(this.<Integer>bindUntilEvent(ActivityEvent.DESTROY))
Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
SystemClock.sleep(10000);
emitter.onNext(1);
}
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.compose(this.<Integer>bindUntilEvent(ActivityEvent.DESTROY))
// .compose(this.<Integer>bindToLifecycle())
.subscribe(new Observer<Integer>() {
@Override
public void onSubscribe(Disposable d) {
AppUtils.log("rxlife","--------onSubscribe");
}
@Override
public void onNext(Integer integer) {
AppUtils.log("rxlife","--------onNext");
}
@Override
public void onError(Throwable e) {
AppUtils.log("rxlife","--------onError");
}
@Override
public void onComplete() {
AppUtils.log("rxlife","--------onComplete");
}
});
使用compose(this.<Integer>bindUntilEvent(ActivityEvent.DESTROY))的结果上面两个方法根据自己需要,用一个就可以了。