andorid jar/库源码解析之RxJava2
RxJava2:
作用:
通过提供一种,观察者和订阅者的模式,的架构,来优化逻辑流程。适用于复杂和需要多数据转换和长流程。
栗子:
定义三个对象类
public class ResultInfo { public int code; public String msg; public String data; } public class UserInfo { public int status; public String name; public String head; public List<SkillInfo> skillInfoList; } public class SkillInfo { public String name; public int level; public SkillInfo(String name, int level){ this.name = name; this.level = level; } }
一段逻辑测试代码:
private ResultInfo login_http(String name, String pwd){ ResultInfo resultInfo = new ResultInfo(); resultInfo.code = 0; resultInfo.msg = ""; Gson gson = new Gson(); UserInfo userInfo = new UserInfo(); userInfo.status = 1; userInfo.name = ""; userInfo.head = ""; userInfo.skillInfoList = new ArrayList<>(); userInfo.skillInfoList.add(new SkillInfo("音乐", 10)); userInfo.skillInfoList.add(new SkillInfo("美术", 6)); userInfo.skillInfoList.add(new SkillInfo("体育", 9)); resultInfo.data = gson.toJson(userInfo); return resultInfo; }
版本1:(定义一个被观察的对象,和订阅者,设置他们使用的线程,最后通过调用 subscribe,使他们关联起来。且执行)
Observable<ResultInfo> observable = Observable.create(new ObservableOnSubscribe<ResultInfo>() { @Override public void subscribe(ObservableEmitter<ResultInfo> e) throws Exception { e.onNext(login_http(name, pwd)); } }); Consumer<ResultInfo> consumer = new Consumer<ResultInfo>() { @Override public void accept(ResultInfo resultInfo) throws Exception { if(resultInfo.code == 0){ Gson gson = new Gson(); UserInfo userInfo = gson.fromJson(resultInfo.data, UserInfo.class); if(userInfo.status == 0){ // 注册 Toast.makeText(getBaseContext(), "注册", Toast.LENGTH_LONG).show(); } else if(userInfo.status == 1){ // 登录 Toast.makeText(getBaseContext(), "登录", Toast.LENGTH_LONG).show(); } } } }; // subscribeOn() 指定的是发送事件的线程, observeOn() 指定的是接收事件的线程. observable.subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(consumer);
版本2:(在版本1的基础上,通过在被观察者调用map处理,观察的数据,使数据只返回结果,而订阅者也是对结果进行判断,这里使用的数据类型发生了改变)。
Observable<Integer> observable = Observable.create(new ObservableOnSubscribe<ResultInfo>() { @Override public void subscribe(ObservableEmitter<ResultInfo> e) throws Exception { e.onNext(login_http(name, pwd)); } }).map(new Function<ResultInfo, Integer>() { @Override public Integer apply(ResultInfo resultInfo) throws Exception { return resultInfo.code; } }); Consumer<Integer> consumer = new Consumer<Integer>() { @Override public void accept(Integer code) throws Exception { if(code == 0){ Toast.makeText(getBaseContext(), "成功", Toast.LENGTH_LONG).show(); } else{ Toast.makeText(getBaseContext(), "失败", Toast.LENGTH_LONG).show(); } } }; // subscribeOn() 指定的是发送事件的线程, observeOn() 指定的是接收事件的线程. observable.subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(consumer);
版本3:(使用RxJava的优势,链式调用,来完成逻辑)
Observable.create(new ObservableOnSubscribe<ResultInfo>() { @Override public void subscribe(ObservableEmitter<ResultInfo> e) throws Exception { e.onNext(login_http(name, pwd)); } }).map(new Function<ResultInfo, Integer>() { @Override public Integer apply(ResultInfo resultInfo) throws Exception { return resultInfo.code; } }).subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<Integer>() { @Override public void accept(Integer code) throws Exception { if(code == 0){ Toast.makeText(getBaseContext(), "成功", Toast.LENGTH_LONG).show(); } else{ Toast.makeText(getBaseContext(), "失败", Toast.LENGTH_LONG).show(); } } });
版本4:(同时引入了flatMap来,处理集合数据,返回集合,每个数据都可以响应订阅者的accept方法,同时引入了filter对数据进行过滤,类似于C#中的,linq用法。)
Observable.create(new ObservableOnSubscribe<ResultInfo>() { @Override public void subscribe(ObservableEmitter<ResultInfo> e) throws Exception { e.onNext(login_http(name, pwd)); } }).map(new Function<ResultInfo, UserInfo>() { @Override public UserInfo apply(ResultInfo resultInfo) throws Exception { UserInfo userInfo = new Gson().fromJson(resultInfo.data, UserInfo.class); return userInfo; } }).flatMap(new Function<UserInfo, Observable<SkillInfo>>() { @Override public Observable<SkillInfo> apply(UserInfo userInfo) throws Exception { return Observable.fromArray(userInfo.skillInfoList.toArray(new SkillInfo[userInfo.skillInfoList.size()])); } }).filter(new Predicate<SkillInfo>() { @Override public boolean test(SkillInfo skillInfo) throws Exception { return skillInfo.level > 8; } }) .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<SkillInfo>() { @Override public void accept(SkillInfo skillInfo) throws Exception { Toast.makeText(getBaseContext(), skillInfo.name + " " + skillInfo.level, Toast.LENGTH_SHORT).show(); } }); // 类似于linq , take, skip deng
版本5:当然,RxJava的用法不仅仅这些,还有没有提到的,take,取几个成员,skip,跳过几个成员。等,这些用法和C#中的linq类似,详细很好理解
// 代码,略
源码解读:
// 源码解读,后面再补上。
源码:https://github.com/ReactiveX/RxJava
引入:
implementation "io.reactivex.rxjava2:rxjava:2.0.7"
implementation "io.reactivex.rxjava2:rxandroid:2.0.1"
RT