參考鏈接:
http://www.lxweimin.com/p/464fa025229e
Rxjava2學習筆記一:RxJava2基本使用
http://www.lxweimin.com/p/cf1dbe7654fc
Rxjava2學習筆記二:RxJava2進階使用-zip操作符
http://www.lxweimin.com/p/ef8b620fdc4c
一.map操作符
-
1.map是Rxjava中的變換操作符,它的作用是對上游發送的每一個事件應用一個函數,使得每一個事件都按照指定的函數變化
Alt text -
2.map中的函數作用是將圓形事件轉換為矩形事件, 從而導致下游接收到的事件就變為了矩形,如下:
Observable.create(new ObservableOnSubscribe<Integer>() { @Override public void subscribe(ObservableEmitter<Integer> emitter) throws Exception { emitter.onNext(1); emitter.onNext(2); emitter.onNext(3); } }).map(new Function<Integer, String>() {//Integer轉換為String @Override public String apply(Integer integer) throws Exception { return "This is result " + integer; } }).subscribe(new Consumer<String>() { @Override public void accept(String s) throws Exception { Log.d(TAG, s); } });
上游我們發送的是數字類型,下游接受的是String類型,中間起轉換作用的就是map操作符(上游發來的事件轉換為任意的類型)。
運行結果:
D/TAG: This is result 1
D/TAG: This is result 2
D/TAG: This is result 3
FlatMap
-
1.FlatMap將一個發送事件的上游Observable變換為多個發送事件的Observables,然后將它們發射的事件合并后放進一個單獨的Observable里,注:concatMap和FlatMap一樣的作用,只是contactMap是有序的,FlatMap是無序的
Alt text
上游發送了三個事件, 分別是1,2,3, 注意它們的顏色
中間flatMap的作用是將圓形的事件轉換為一個發送矩形事件和三角形事件的新的上游Observable
eg:Observable.create(new ObservableOnSubscribe<Integer>() { @Override public void subscribe(ObservableEmitter<Integer> emitter) throws Exception { emitter.onNext(1); emitter.onNext(2); emitter.onNext(3); } }).flatMap(new Function<Integer, ObservableSource<String>>() { @Override public ObservableSource<String> apply(Integer integer) throws Exception { final List<String> list = new ArrayList<>(); for (int i = 0; i < 3; i++) { list.add("I am value " + integer); } return Observable.fromIterable(list).delay(10,TimeUnit.MILLISECONDS); } }).subscribe(new Consumer<String>() { @Override public void accept(String s) throws Exception { Log.d(TAG, s); } });
打印結果:
D/TAG: I am value 1
D/TAG: I am value 1
D/TAG: I am value 1
D/TAG: I am value 3
D/TAG: I am value 3
D/TAG: I am value 3
D/TAG: I am value 2
D/TAG: I am value 2
D/TAG: I am value 2
實際使用場景
1.需求:嵌套的網絡請求, 首先需要去請求注冊, 待注冊成功回調了再去請求登錄的接口
-
2.實現
接口-public interface Api { @GET Observable<LoginResponse> login(@Body LoginRequest request); @GET Observable<RegisterResponse> register(@Body RegisterRequest request); }
網絡請求-
api.register(new RegisterRequest()) //發起注冊請求 .subscribeOn(Schedulers.io()) //在IO線程進行網絡請求 .observeOn(AndroidSchedulers.mainThread()) //回到主線程去處理請求注冊結果 .doOnNext(new Consumer<RegisterResponse>() { @Override public void accept(RegisterResponse registerResponse) throws Exception { //先根據注冊的響應結果去做一些操作 } }) .observeOn(Schedulers.io()) //回到IO線程去發起登錄請求 .flatMap(new Function<RegisterResponse, ObservableSource<LoginResponse>>() { @Override public ObservableSource<LoginResponse> apply(RegisterResponse registerResponse) throws Exception { return api.login(new LoginRequest());//發起登陸請求 } }) .observeOn(AndroidSchedulers.mainThread()) //回到主線程去處理請求登錄的結果 .subscribe(new Consumer<LoginResponse>() { @Override public void accept(LoginResponse loginResponse) throws Exception { Toast.makeText(MainActivity.this, "登錄成功", Toast.LENGTH_SHORT).show(); } }, new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { Toast.makeText(MainActivity.this, "登錄失敗", Toast.LENGTH_SHORT).show(); } });