0. 为了月薪1.8万
奋斗方向和目标.png1. 序言
- 上一篇博客讲述的是过滤操作符,如需了解,请点击跳转阅读:https://www.jianshu.com/p/154da44670de
2. 组合操作符
- startWith:在源Observable数据前插上一些数据:
Observable.just("Hello", "World", "Hello").startWith("Beijing", "上海", "广州")
.subscribe(new Action1<String>() {
@Override
public void call(String s) {
Log.i(TAG, "Action1: String:" + s);
}
});
05-18 20:46:32.343 1658-1658/com.best.testrxjava I/MainActivity: Action1: String:Beijing
05-18 20:46:32.343 1658-1658/com.best.testrxjava I/MainActivity: Action1: String:上海
05-18 20:46:32.343 1658-1658/com.best.testrxjava I/MainActivity: Action1: String:广州
05-18 20:46:32.343 1658-1658/com.best.testrxjava I/MainActivity: Action1: String:Hello
05-18 20:46:32.343 1658-1658/com.best.testrxjava I/MainActivity: Action1: String:World
05-18 20:46:32.344 1658-1658/com.best.testrxjava I/MainActivity: Action1: String:Hello
- merge:
- 将多个Observable合并为一个Observable。
Observable<String> obs1 = Observable.just("A", "B", "C");
Observable<String> obs2 = Observable.just("D", "E", "F");
Observable<String> obs3 = Observable.just("G", "H", "I");
Observable<String> obs4 = Observable.just("G", "K", "L");
Observable.merge(obs1,obs2,obs3,obs4).subscribe(new Action1<String>() {
@Override
public void call(String s) {
Log.i(TAG, "Action1: String:" + s);
}
});
05-18 21:05:20.709 2032-2032/com.best.testrxjava I/MainActivity: Action1: String:A
05-18 21:05:20.709 2032-2032/com.best.testrxjava I/MainActivity: Action1: String:B
05-18 21:05:20.709 2032-2032/com.best.testrxjava I/MainActivity: Action1: String:C
05-18 21:05:20.709 2032-2032/com.best.testrxjava I/MainActivity: Action1: String:D
05-18 21:05:20.709 2032-2032/com.best.testrxjava I/MainActivity: Action1: String:E
05-18 21:05:20.710 2032-2032/com.best.testrxjava I/MainActivity: Action1: String:F
05-18 21:05:20.710 2032-2032/com.best.testrxjava I/MainActivity: Action1: String:G
05-18 21:05:20.710 2032-2032/com.best.testrxjava I/MainActivity: Action1: String:H
05-18 21:05:20.710 2032-2032/com.best.testrxjava I/MainActivity: Action1: String:I
05-18 21:05:20.710 2032-2032/com.best.testrxjava I/MainActivity: Action1: String:G
05-18 21:05:20.710 2032-2032/com.best.testrxjava I/MainActivity: Action1: String:K
05-18 21:05:20.710 2032-2032/com.best.testrxjava I/MainActivity: Action1: String:L
- merge合并可能发生错乱(Observable订阅的线程不同时)
Observable<String> obs1 = Observable.just("A", "B", "C").subscribeOn(Schedulers.io());
Observable<String> obs2 = Observable.just("D", "E", "F").subscribeOn(Schedulers.io());
Observable<String> obs3 = Observable.just("G", "H", "I");
Observable.merge(obs1,obs2,obs3).subscribe(new Action1<String>() {
@Override
public void call(String s) {
Log.i(TAG, "Action1: String:" + s);
}
});
05-18 21:21:44.646 2489-2489/? I/MainActivity: Action1: String:G
05-18 21:21:44.646 2489-2489/? I/MainActivity: Action1: String:H
05-18 21:21:44.646 2489-2489/? I/MainActivity: Action1: String:I
05-18 21:21:44.646 2489-2505/? I/MainActivity: Action1: String:A
05-18 21:21:44.647 2489-2505/? I/MainActivity: Action1: String:B
05-18 21:21:44.647 2489-2505/? I/MainActivity: Action1: String:C
05-18 21:21:44.648 2489-2505/? I/MainActivity: Action1: String:D
05-18 21:21:44.648 2489-2505/? I/MainActivity: Action1: String:E
05-18 21:21:44.648 2489-2505/? I/MainActivity: Action1: String:F
- concat:将多个Observable发射的数据进行合并,而且不会乱序:
Observable<String> obs1 = Observable.just("A", "B", "C").subscribeOn(Schedulers.io());
Observable<String> obs2 = Observable.just("D", "E", "F").subscribeOn(Schedulers.io());
Observable<String> obs3 = Observable.just("G", "H", "I");
Observable.concat(obs1,obs2,obs3).subscribe(new Action1<String>() {
@Override
public void call(String s) {
Log.i(TAG, "Action1: String:" + s);
}
});
05-18 21:28:06.152 2592-2606/? I/MainActivity: Action1: String:A
05-18 21:28:06.152 2592-2606/? I/MainActivity: Action1: String:B
05-18 21:28:06.152 2592-2606/? I/MainActivity: Action1: String:C
05-18 21:28:06.152 2592-2606/? I/MainActivity: Action1: String:D
05-18 21:28:06.152 2592-2606/? I/MainActivity: Action1: String:E
05-18 21:28:06.152 2592-2606/? I/MainActivity: Action1: String:F
05-18 21:28:06.152 2592-2606/? I/MainActivity: Action1: String:G
05-18 21:28:06.152 2592-2606/? I/MainActivity: Action1: String:H
05-18 21:28:06.152 2592-2606/? I/MainActivity: Action1: String:I
- zip:合并两个或多个Observable发射出的数据项,根据指定的函数变换他们,并发射一个新值。
Observable<String> obs1 = Observable.just("A", "B", "C").subscribeOn(Schedulers.io());
Observable<String> obs2 = Observable.just("D", "E", "F").subscribeOn(Schedulers.io());
Observable.zip(obs1, obs2, new Func2<String, String, String>() {
@Override
public String call(String s, String s2) {
return s + s2;
}
}).subscribe(new Action1<String>() {
@Override
public void call(String s) {
Log.d(TAG, "zip: " + s);
}
});
05-18 22:50:14.022 3063-3079/com.best.testrxjava D/MainActivity: zip: AD
05-18 22:50:14.022 3063-3079/com.best.testrxjava D/MainActivity: zip: BE
05-18 22:50:14.022 3063-3079/com.best.testrxjava D/MainActivity: zip: CF
- combineLastest:
Observable<String> obs1 = Observable.just("A", "B", "C").subscribeOn(Schedulers.io());
Observable<String> obs2 = Observable.just("D", "E", "F").subscribeOn(Schedulers.io());
Observable.combineLatest(obs1, obs2, new Func2<String, String, String>() {
@Override
public String call(String s, String s2) {
return s + s2;
}
}).subscribe(new Action1<String>() {
@Override
public void call(String s) {
Log.d(TAG, "combineLatest: " + s);
}
});
05-18 22:56:52.381 3241-3258/com.best.testrxjava D/MainActivity: combineLatest: CD
05-18 22:56:52.381 3241-3258/com.best.testrxjava D/MainActivity: combineLatest: CE
05-18 22:56:52.381 3241-3258/com.best.testrxjava D/MainActivity: combineLatest: CF
说明:与zip不同的是:
- combineLastest结合最新的数进行合并,比如obs1中的"C"是最近发射的,最近发射的和obs2中的所有数据进行合并。
- zip指的是多个Observable的合并处理,而combineLastest指的是两个Observable的合并处理。
3. 推荐博客:
如果Recyclerview基础不太好,欢迎跳转阅读 https://www.jianshu.com/p/3eb81f50f4db
4. 后续
如果大家喜欢这篇文章,欢迎点赞;如果想看更多前端移动端后端Java或Python方面的技术,欢迎关注!
网友评论