• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

MVP-1封装-使用弱引用和rxjava解决内存泄漏问题

武飞扬头像
_yao_
帮助1

一.Model层封装

1.RetrofitManager:懒汉双重锁

public class RetrofitManager {
    //单例模式:懒汉双重锁校验 节省资源
    private RetrofitManager(){}
    private static RetrofitManager sRetrofitManager;//区分成员变量m 静态变量s 局部变量
    public static RetrofitManager getInstance(){
        if(sRetrofitManager == null){
            synchronized (RetrofitManager.class){
                if(sRetrofitManager == null){
                    sRetrofitManager = new RetrofitManager();
                }
            }
        }
        return sRetrofitManager;
    }

    private Retrofit mRetrofit;
    public Retrofit getRetrofit() {
        if(mRetrofit == null){
           createRetrofit();
        }
        return mRetrofit;
    }
    //构建Retrofit对象
    private void createRetrofit() {
        mRetrofit = new Retrofit.Builder()
                .baseUrl("http://82.156.173.100:8087")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
    }
}
学新通

2.ApiServer

public interface ApiServer {
    //分页加载
    @GET("video/findVideos?pageSize=200")
    Observable<VideoEntity> video(@Query("currentPage") int currentPage);
}

3.BaseModel

//网络请求
public class BaseModel {
    //网络请求接口文档 private:只能被本类访问  protected:被本类以及实现子类访问
    protected ApiServer mApiServer;
    public BaseModel() {
        mApiServer = RetrofitManager.getInstance().getRetrofit().create(ApiServer.class);
    }
}

二.Presenter层封装

//绑定解析View 以图和数据交互
public class BasePresenter<V> {
    //用弱引用创建View

    //弱引用 ->解决MVP的内存泄露->OOM内存溢出
    //强 软 弱 虚
    WeakReference<V> mWeakReference;
    //请求方式 okHttp retrofit rxjava
    //每个请求 都会产生订阅->把订阅做一个统一的管理

    //将rxjava的 订阅 添加到mCompositeDisposable 通过mCompositeDisposable 对订阅的请求做统一的管理
    protected CompositeDisposable mCompositeDisposable = new CompositeDisposable();

//    通过mWeakReference创建View
    public void attachView(V view){
        mWeakReference = new WeakReference<V>(view);
    }
    //取出对应的view
    protected V getView(){
        return mWeakReference == null?null:mWeakReference.get();
    }
    //在咱们取出对应的view之前做这个判断
    protected boolean isViewAttached(){
        return mWeakReference != null && mWeakReference.get() !=null;
    }

    //在APP或者页面的生命周期结束 及其他需要的地方 释放View 取消订阅

    public void detachView(){
//        释放View
        if (mWeakReference != null){
            mWeakReference.clear();
            mWeakReference = null;
        }
//        取消订阅
        mCompositeDisposable.clear();

    }
}
学新通

三.View层封装

1.IBaseView

//展示 view最基础的能力
public interface IBaseView {
    //加载中
    void onLoading();
    //加载成功
    void onLoadSuccess();
    //加载失败
    void onLoadFailed();
}

2.BaseActivity

public abstract   class BaseActivity<V,P extends BasePresenter<V>> extends AppCompatActivity implements IBaseView{
    protected  P mPresenter;
    //初始化数据:创建mPresenter
    protected abstract void initData();
    //初始化
    protected abstract void initView(Bundle savedInstanceState);
    //布局id
    protected abstract int bindLayout();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(bindLayout());
        initView(savedInstanceState);
        initData();
        //使用弱引用创建View
        if(mPresenter != null){
            mPresenter.attachView((V)this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //释放view 取消订阅
        if(mPresenter != null){
            mPresenter.detachView();
        }
    }

    @Override
    public void onLoading() {
        Toast.makeText(this, "数据加载中", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onLoadSuccess() {
        Toast.makeText(this, "数据加载成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onLoadFailed() {
        Toast.makeText(this, "数据加载失败", Toast.LENGTH_SHORT).show();
    }
}
学新通

3.BaseFragment

public abstract class BaseFragment<V,P extends BasePresenter<V>> extends Fragment implements IBaseView {
    protected  P mPresenter;
    protected abstract void initData();
    protected abstract void initView();
    protected abstract int bindLayout();
    protected  View view;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(bindLayout(),container,false);
        initView();
        initData();
        //使用弱引用创建View
        if(mPresenter != null){
            mPresenter.attachView((V)this);
        }
        return view;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null){
            //释放订阅 销毁view
            mPresenter.detachView();
        }
    }
    //多的方法:findViewById
    public <T extends View> T findViewById(@IdRes int id) {
        return mView.findViewById(id);
    }

    @Override
    public void onLoading() {
        Toast.makeText(getContext(), "数据加载中", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onLoadSuccess() {
        Toast.makeText(getContext(), "数据加载成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onLoadFailed() {
        Toast.makeText(getContext(), "数据加载失败", Toast.LENGTH_SHORT).show();
    }
}
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgccbhe
系列文章
更多 icon
同类精品
更多 icon
继续加载