Rxjava解决内存泄漏

package com.bw.rxjava.activity;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

import com.bw.rxjava.R;
import com.bw.rxjava.adapter.MyAdapter;
import com.bw.rxjava.api.Api;
import com.bw.rxjava.api.ApiService;
import com.bw.rxjava.bean.JokeInfo;
import com.bw.rxjava.bean.Weal;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import java.util.List;
import java.util.Map;

import io.reactivex.CompletableOnSubscribe;
import io.reactivex.Flowable;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * https://blog.csdn.net/qq_23179075/article/details/79256089
 * http://06peng.com/archives/tag/compositedisposable
 * 解决内存泄漏问题
 */
public class MainActivity extends AppCompatActivity {
    //订阅管理器
    CompositeDisposable compositeDisposable =new CompositeDisposable();
    private ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lv = findViewById(R.id.lv);

        //  getWeal();

        getJoke();
    }

    private void getJoke() {

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Api.jokeUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        ApiService apiService = retrofit.create(ApiService.class);

        //通过retrofit创建被观察者Flowable

        Flowable<List<JokeInfo>> flowable = apiService.getJoke();

        //得到订阅者
        DisposableSubscriber<List<JokeInfo>> disposableSubscriber = flowable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())

                .subscribeWith(new DisposableSubscriber<List<JokeInfo>>() {
                    @Override
                    public void onNext(List<JokeInfo> jokeInfos) {
                        Log.i("xxx", jokeInfos.size() + "");
                        MyAdapter adapter = new MyAdapter(MainActivity.this, jokeInfos);
                        lv.setAdapter(adapter);
                    }

                    @Override
                    public void onError(Throwable t) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
        //添加订阅者到订阅管理器里面
        compositeDisposable.add(disposableSubscriber);
    }

    private void getWeal() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Api.wealUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        ApiService apiService = retrofit.create(ApiService.class);

        //通过retrofit创建被观察者Flowable

        Flowable<Weal> flowable = apiService.getWeal(1);
        DisposableSubscriber<Weal> disposableSubscriber = flowable.subscribeOn(Schedulers.io())//指定发送事件的线程,io线程
                .map(new Function<Weal, Weal>() {//一对一的转换
                    @Override
                    public Weal apply(Weal weal) throws Exception {


                        return weal;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())//接收事件在主线程
                .subscribeWith(new DisposableSubscriber<Weal>() {

                    @Override
                    public void onNext(Weal weal) {//接收成功的事件
                        Log.i("xxx", weal.getResults().get(0).getWho());
                    }

                    @Override
                    public void onError(Throwable t) {//接收失败的事件
                        Log.i("xxx", t.toString());
                    }

                    @Override
                    public void onComplete() {//事件完成

                    }
                });
        //添加订阅者到订阅管理器里面
        compositeDisposable.add(disposableSubscriber);

    }

    //取消订阅
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //判断是否解除订阅
        boolean disposed = compositeDisposable.isDisposed();
        Log.i("xxx","disposed:"+disposed);


        if (!disposed){
            //消除订阅
            compositeDisposable.clear();

            //解除订阅
            compositeDisposable.dispose();
            Log.i("xxx","解绑了");


        }

    }
}

  • 1
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值