目錄:andorid jar/庫源碼解析 RxJava2: 作用: 通過提供一種,觀察者和訂閱者的模式,的架構,來優化邏輯流程。適用於複雜和需要多數據轉換和長流程。 慄子: 定義三個對象類 public class ResultInfo { public int code; public Strin ...
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"