RxAndroid 应用(三)(简易天气App之Observable内存泄露)

       RxAndroid 应用(一)(简易天气App)

       RxAndroid 应用(二)(简易天气App)

       RxAndroid 应用(三)(简易天气App之Observable内存泄露)

       RxAndroid 应用(四)(简易天气App之RxBus)


在我们的Handler使用不当会发生内存泄露,那么Observable会不会发生持有context而导致内存泄露呢?答案是肯定的,接下来就来看看怎样避免。

public class RxUtils {

    public static void unsubscribeIfNotNull(Subscription subscription) {
        if (subscription != null) {
            subscription.unsubscribe();
        }
    }

    public static CompositeSubscription getNewCompositeSubIfUnsubscribed(CompositeSubscription subscription) {
        if (subscription == null || subscription.isUnsubscribed()) {
            return new CompositeSubscription();
        }

        return subscription;
    }
}
CompositeSubscription内部有一个Subscription的Set对象的集合,可以调用unsubscription()方法取消集合中所有subcription的订阅。


public class BasePresenterImpl implements BasePresenter {
    protected CompositeSubscription mSubscriptions = new CompositeSubscription();

    @Override
    public void onCreate() {
        mSubscriptions = RxUtils.getNewCompositeSubIfUnsubscribed(mSubscriptions);
    }

    @Override
    public void onResume() {

    }

    @Override
    public void onPause() {

    }

    @Override
    public void onDestroy() {
        mSubscriptions.unsubscribe();
    }
}

public class MainPresenterImpl extends BasePresenterImpl implements MainPresenter {
    private final Logger mLogger = Logger.getLogger(getClass());

    private MainView mMainView;

    public MainPresenterImpl(MainView mainView) {
        mMainView = mainView;
    }

    @Override
    public void getWeatherData(String place) {
        if (TextUtils.isEmpty(place)) {
            return;
        }
        mMainView.showProgress();
        mSubscriptions.add(ServiceManager.getInstance().getApiService().getWeatherInfo(place, Constants.BAIDU_AK)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<WeatherResponse>() {
                    @Override
                    public void onCompleted() {
                        mMainView.hideProgress();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mLogger.error(e.getMessage(), e);
                        mMainView.hideProgress();
                    }

                    @Override
                    public void onNext(WeatherResponse weatherResponse) {
                        mMainView.setupWeatherData(weatherResponse);
                    }
                }));
    }

    @Override
    public void getPlaceData() {
        PlaceRepository repository = new PlaceRepository();
        mSubscriptions.add(repository.getPlaceList(BaseApplication.getInstance())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<List<Place>>() {
                    @Override
                    public void onNext(List<Place> places) {
                        mMainView.setupPlaceData(places);
                    }

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mLogger.error(e.getMessage(), e);
                    }
                }));
    }

    @Override
    public void getPlaceAndWeatherData(String place) {
        mMainView.showProgress();
        PlaceRepository repository = new PlaceRepository();
        Context context = BaseApplication.getInstance();
        Observable placeObservable = repository.getPlaceList(context);
        Observable weatherObservable =  ServiceManager.getInstance().getApiService().getWeatherInfo(place, Constants.BAIDU_AK);
        mSubscriptions.add(Observable.merge(placeObservable, weatherObservable)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Object>() {
                    @Override
                    public void onCompleted() {
                        mMainView.hideProgress();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mLogger.error(e.getMessage(), e);
                        mMainView.hideProgress();
                    }

                    @Override
                    public void onNext(Object obj) {
                        if (obj instanceof List) {
                            mMainView.setupPlaceData((List<Place>) obj);
                        } else if (obj instanceof WeatherResponse) {
                            mMainView.setupWeatherData((WeatherResponse) obj);
                        }
                    }
                }));
    }
}
如上代码所述,我们将所有Observable对象添加到CompositeSubscription中统一管理,避免产生Observable内存泄露情况


代码地址:https://github.com/mickyliu945/CommonProj

你可能感兴趣的:(RxAndroid 应用(三)(简易天气App之Observable内存泄露))