參考鏈接:
http://www.itdecent.cn/p/464fa025229e
Rxjava2學(xué)習(xí)筆記一:RxJava2基本使用
http://www.itdecent.cn/p/cf1dbe7654fc
Rxjava2學(xué)習(xí)筆記二:RxJava2進(jìn)階使用-zip操作符
http://www.itdecent.cn/p/ef8b620fdc4c
一.map操作符
-
1.map是Rxjava中的變換操作符,它的作用是對(duì)上游發(fā)送的每一個(gè)事件應(yīng)用一個(gè)函數(shù),使得每一個(gè)事件都按照指定的函數(shù)變化
Alt text -
2.map中的函數(shù)作用是將圓形事件轉(zhuǎn)換為矩形事件, 從而導(dǎo)致下游接收到的事件就變?yōu)榱司匦危缦拢?/p>
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轉(zhuǎn)換為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); } });上游我們發(fā)送的是數(shù)字類型,下游接受的是String類型,中間起轉(zhuǎn)換作用的就是map操作符(上游發(fā)來的事件轉(zhuǎn)換為任意的類型)。
運(yùn)行結(jié)果:
D/TAG: This is result 1
D/TAG: This is result 2
D/TAG: This is result 3
FlatMap
-
1.FlatMap將一個(gè)發(fā)送事件的上游Observable變換為多個(gè)發(fā)送事件的Observables,然后將它們發(fā)射的事件合并后放進(jìn)一個(gè)單獨(dú)的Observable里,注:concatMap和FlatMap一樣的作用,只是contactMap是有序的,F(xiàn)latMap是無序的
Alt text
上游發(fā)送了三個(gè)事件, 分別是1,2,3, 注意它們的顏色
中間flatMap的作用是將圓形的事件轉(zhuǎn)換為一個(gè)發(fā)送矩形事件和三角形事件的新的上游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); } });
打印結(jié)果:
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
實(shí)際使用場(chǎng)景
1.需求:嵌套的網(wǎng)絡(luò)請(qǐng)求, 首先需要去請(qǐng)求注冊(cè), 待注冊(cè)成功回調(diào)了再去請(qǐng)求登錄的接口
-
2.實(shí)現(xiàn)
接口-public interface Api { @GET Observable<LoginResponse> login(@Body LoginRequest request); @GET Observable<RegisterResponse> register(@Body RegisterRequest request); }網(wǎng)絡(luò)請(qǐng)求-
api.register(new RegisterRequest()) //發(fā)起注冊(cè)請(qǐng)求 .subscribeOn(Schedulers.io()) //在IO線程進(jìn)行網(wǎng)絡(luò)請(qǐng)求 .observeOn(AndroidSchedulers.mainThread()) //回到主線程去處理請(qǐng)求注冊(cè)結(jié)果 .doOnNext(new Consumer<RegisterResponse>() { @Override public void accept(RegisterResponse registerResponse) throws Exception { //先根據(jù)注冊(cè)的響應(yīng)結(jié)果去做一些操作 } }) .observeOn(Schedulers.io()) //回到IO線程去發(fā)起登錄請(qǐng)求 .flatMap(new Function<RegisterResponse, ObservableSource<LoginResponse>>() { @Override public ObservableSource<LoginResponse> apply(RegisterResponse registerResponse) throws Exception { return api.login(new LoginRequest());//發(fā)起登陸請(qǐng)求 } }) .observeOn(AndroidSchedulers.mainThread()) //回到主線程去處理請(qǐng)求登錄的結(jié)果 .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(); } });

