文章目录
1、RxJava的介绍
RxJava
基于事件流,实现异步操作的一个强大的、高效的、简单的
开源库,特别是基于链式调用的编写风格,在处理简单,复杂的逻辑中都能让你知道我是谁,我在那,我在干什么。在开发中一般和okHttp
+retrofit
结合使用。
2、RxJava基本使用
RxJava采用观察者模式来实现事件的传递,首先需要有个观察者
然后需要个被观察者
其次将被观察者
和观察者
建立联系,然后被观察者
发送事件,观察者
监听到事件然后对事件进行处理。
创建观察者 (Observable)
//被观察者
Observable<String> observable = Observable.create(new ObservableOnSubscribe<String>() {
@Override
public void subscribe(ObservableEmitter<String> e) throws Exception {
//发送普通事件
e.onNext("A");
e.onNext("B");
//发送事件结束事件
e.onComplete();
}
});
通过 Observable.create 创建被观察者,ObservableEmitter
主要负责事件的发送
创建观察者(Observer)
// 观察者
Observer<String> observer = new Observer<String>() {
Disposable mDisposable;
@Override
public void onSubscribe(Disposable d) {
//Disposable 用来中断事件发送API
mDisposable = d;
Log.d("TAG", "开始subscribe连接");
}
@Override
public void onNext(String value) {
if ("A".equals(value)) {
mDisposable.dispose();
}
Log.d("TAG", "对Next事件作出响应" + value);
}
@Override
public void onError(Throwable e) {
Log.d("TAG", "onError事件" + e);
}
@Override
public void onComplete() {
Log.d("TAG", "onComplete事件");
}
};
onNext
接收事件,onError
接受被观察者发送过来的错误事件,onComplete
接收被观察者发送过来的onComplete
事件,表明事件结束。
最后通过subscribe
操作符 将被观察和观察者进行联系起来。
// 将被观察和观察者进行关联
observable.subscribe(observer);
之前也说过RXJava通过链式调用,可以让代码更简洁明了,下面就看下链式调用的结构
Observable.create(new ObservableOnSubscribe<String>() {
@Override
public void subscribe(ObservableEmitter<String> e) throws Exception {
e.onNext("A");
e.onNext("B");
e.onComplete();
}
}).subscribe(new Observer<String>() {
Disposable mDisposable;
@Override
public void onSubscribe(Disposable d) {
mDisposable = d;
Log.d("TAG", "开始采用subscribe连接");
}
@Override
public void onNext(String value) {
if ("A".equals(value)) {
mDisposable.dispose();
}
Log.d("TAG", "对Next事件作出响应" + value);
}
@Override
public void onError(Throwable e) {
Log.d("TAG", "onError事件" + e);
}
@Override
public void onComplete() {
Log.d("TAG", "onComplete事件");
}
});
两种方式效果都是一样,只不过基于链式调用的看起来比较简单明了。
举个简单的例子,将老师比如成被观察者
,学生比喻成观察者
,那么学校
就是连接老师和学生的纽带,老师上课传授知识,学生负责接收老师传授的知识。
public final Disposable subscribe() {}
// 表示观察者不对被观察者发送的事件作出任何响应(但被观察者还是可以继续发送事件)
public final Disposable subscribe(Consumer<? super T> onNext) {}
// 表示观察者只对被观察者发送的Next事件作出响应
public final Disposable subscribe(Consumer<? super T> onNext, Consumer<? super Throwable> onError) {}
// 表示观察者只对被观察者发送的Next事件 & Error事件作出响应
public final Disposable subscribe(Consumer<? super T> onNext, Consumer<? super Throwable> onError, Action onComplete) {}
// 表示观察者只对被观察者发送的Next事件、Error事件 & Complete事件作出响应
public final Disposable subscribe(Consumer<? super T> onNext, Consumer<? super Throwable> onError, Action onComplete, Consumer<? super Disposable> onSubscribe) {}
// 表示观察者只对被观察者发送的Next事件、Error事件 、Complete事件 & onSubscribe事件作出响应
public final void subscribe(Observer<? super T> observer) {}
// 表示观察者对被观察者发送的任何事件都作出响应
3、 RxJava创建操作符使用介绍
在上边介绍了RXJava的一个基本操作,显然是不够的,接下来来了解下Observable
被观察者创建除了create
方式 还能那些能创建。
以下方法都是通过Observable 的静态方法创建d
。
3.1 、 普通的创建操作符
3.1.1 、 创建操作符 create
方法 | 介绍 |
---|---|
just(…) | 可以传入多个对象参数,参数个数<=10,相当于直接调用 onNext方法发送事件 |
Observable.just("A", "b").subscribe(new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
Log.e("TAG", "最先执行的方法......onSubscribe");
}
@Override
public void onNext(String value) {
Log.e("TAG", "事件接收者::::" + value);
}
@Override
public void onError(Throwable e) {
Log.e("TAG", "onError::::" + e);
}
@Override
public void onComplete() {
Log.e("TAG", "onComplete::::");
}
});
输入log
2019-08-02 10:57:24.805 5564-5564/camera.test.com.networkdata E/TAG: 最先执行的方法......onSubscribe
2019-08-02 10:57:24.806 5564-5564/camera.test.com.networkdata E/TAG: 事件接收者::::A
2019-08-02 10:57:24.806 5564-5564/camera.test.com.networkdata E/TAG: 事件接收者::::b
2019-08-02 10:57:24.806 5564-5564/camera.test.com.networkdata E/TAG: onComplete::::
3.1.2 、创建操作符 fromArray
方法 | 介绍 |
---|---|
fromArray (…) | 参数可以是一个数组类型,也可以是一个int类型,just最终调用的也是fromArray 方法 |
Integer age[] = {1, 2, 3, 4};
Observable.fromArray(age).subscribe(new Observer<Integer>() {
@Override
public void onSubscribe(Disposable d) {
Log.e("TAG", "Rx2FromArray最先执行的方法......onSubscribe");
}
@Override
public void onNext(Integer value) {
Log.e("TAG", "Rx2FromArray::::onNext" + value);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
Log.e("TAG", "Rx2FromArray::::onComplete");
}
});
输出log
2019-08-02 11:17:41.781 5564-5564/camera.test.com.networkdata E/TAG: Rx2FromArray最先执行的方法......onSubscribe
2019-08-02 11:17:41.781 5564-5564/camera.test.com.networkdata E/TAG: Rx2FromArray::::onNext1
2019-08-02 11:17:41.781 5564-5564/camera.test.com.networkdata E/TAG: Rx2FromArray::::onNext2
2019-08-02 11:17:41.781 5564-5564/camera.test.com.networkdata E/TAG: Rx2FromArray::::onNext3
2019-08-02 11:17:41.781 5564-5564/camera.test.com.networkdata E/TAG: Rx2FromArray::::onNext4
2019-08-02 11:17:41.782 5564-5564/camera.test.com.networkdata E/TAG: Rx2FromArray::::onComplete
3.1.3 、创建操作符 fromIterable
方法 | 介绍 |
---|---|
fromIterable(Iterable<? extends T> source) | 通过传入一个Iterable类型的对象创建 被观察者 |
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("A");
arrayList.add("B");
arrayList.add("C");
Observable.fromIterable(arrayList).subscribe(new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(String value) {
Log.e("TAG", "Rx2fromIterable::::onNext" + value);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
Log.e("TAG", "Rx2fromIterable::::onComplete");
}
});
log输出
2019-08-02 11:33:20.602 5564-5564/camera.test.com.networkdata E/TAG: Rx2fromIterable::::onNextA
2019-08-02 11:33:20.602 5564-5564/camera.test.com.networkdata E/TAG: Rx2fromIterable::::onNextB
2019-08-02 11:33:20.602 5564-5564/camera.test.com.networkdata E/TAG: Rx2fromIterable::::onNextC
2019-08-02 11:33:20.602 5564-5564/camera.test.com.networkdata E/TAG: Rx2fromIterable::::onComplete
3.1.4 、创建操作符 range/rangeLong
方法 | 介绍 |
---|---|
range(final int start, final int count) | 创建一个被观察者,连续发送一系列事件,start 开始数据,count发送事件个数 |
rangeLong | 通上,只是数据类型为long |
// 参数1 = 事件序列起始点;
// 参数2 = 事件数量;
// 注:若设置为负数,则会抛出异常
Observable.range(1, 5).subscribe(new Observer<Integer>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Integer value) {
Log.e("TAG", "onNext:::::::::::" + value + "::range:::::::::::::" + Thread.currentThread());
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
log输出
2019-08-02 11:49:24.595 5564-5564/camera.test.com.networkdata E/TAG: onNext:::::::::::1::range:::::::::::::Thread[main,5,main]
2019-08-02 11:49:24.596 5564-5564/camera.test.com.networkdata E/TAG: onNext:::::::::::2::range:::::::::::::Thread[main,5,main]
2019-08-02 11:49:24.597 5564-5564/camera.test.com.networkdata E/TAG: onNext:::::::::::3::range:::::::::::::Thread[main,5,main]
2019-08-02 11:49:24.597 5564-5564/camera.test.com.networkdata E/TAG: onNext:::::::::::4::range:::::::::::::Thread[main,5,main]
2019-08-02 11:49:24.597 5564-5564/camera.test.com.networkdata E/TAG: onNext:::::::::::5::range:::::::::::::Thread[main,5,main]
3.2、 特殊创建操作符
方法 | 介绍 |
---|---|
empty | 创建一个 仅发送Complete事件 的被观察者(Observable) |
error | 创建一个 仅发送Error事件,直接通知异常 的被观察者(Observable) |
never | 创建一个 不发送任何事件 的被观察者(Observable) |
3.3、 延迟创建操作符
3.3.1、 创建操作符 defer
方法 | 介绍 |
---|---|
defer | 直到有观察者(Observer )订阅时,才动态创建被观察者对象(Observable) & 发送事件 |
Observable<Integer> defer = Observable.defer(new Callable<ObservableSource<Integer>>() {
@Override
public ObservableSource<Integer> call() throws Exception {
return Observable.just(num);
}
});
num = 90;
defer.subscribe(new Observer<Integer>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Integer value) {
Log.e("TAG", "onNext::::defer" + value);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
log输出
2019-08-02 11:54:11.752 5564-5564/camera.test.com.networkdata E/TAG: onNext::::defer90
3.3.2、 创建操作符 timer
方法 | 介绍 |
---|---|
timer | 创建一个被观察者对象(Observable),延迟指定时间 发送事件 |
//延迟三秒发送一个0事件
Observable.timer(3, TimeUnit.SECONDS).subscribe(new Observer<Long>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Long value) {
Log.e("TAG", "onNext::::timer" + value);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
log输出
2019-08-02 11:54:09.265 5564-7139/camera.test.com.networkdata E/TAG: onNext::::timer0
3.4、 循环创建操作符
3.4.1、 创建操作符 interval
方法 | 介绍 |
---|---|
interval | 每隔指定时间 就发送 事件的被观察者对象(Observable) |
// 参数1 = 第1次延迟时间;
// 参数2 = 间隔时间数字;
// 参数3 = 时间单位;
Observable.interval(2, 2, TimeUnit.SECONDS).subscribe(new Observer<Long>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Long value) {
Log.e("TAG", "onNext:::::::::::" + value + ":::::::::::::" + Thread.currentThread());
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
log:一直不停的发送事件
2019-08-02 11:58:01.324 5564-7222/camera.test.com.networkdata E/TAG: onNext:::::::::::0:::::::::::::Thread[RxComputationThreadPool-3,5,main]
2019-08-02 11:58:03.322 5564-7222/camera.test.com.networkdata E/TAG: onNext:::::::::::1:::::::::::::Thread[RxComputationThreadPool-3,5,main]
2019-08-02 11:58:05.321 5564-7222/camera.test.com.networkdata E/TAG: onNext:::::::::::2:::::::::::::Thread[RxComputationThreadPool-3,5,main]
..........
..........
3.4.2 、创建操作符 intervalRange
方法 | 介绍 |
---|---|
intervalRange | 每隔指定时间 就发送 事件的被观察者对象(Observable) 可指定发送的数据的数量 |
// 参数1 = 事件序列起始点;
// 参数2 = 事件数量;
// 参数3 = 第1次事件延迟发送时间;
// 参数4 = 间隔时间发送数据;
// 参数5 = 时间单位
Observable.intervalRange(1, 5, 0, 1, TimeUnit.SECONDS).subscribe(new Observer<Long>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Long value) {
Log.e("TAG", "onNext:::::::::::" + value + "::intervalRange:::::::::::::" + Thread.currentThread());
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
2019-08-02 12:20:37.628 7835-7915/camera.test.com.networkdata E/TAG: onNext:::::::::::1::intervalRange:::::::::::::Thread[RxComputationThreadPool-1,5,main]
2019-08-02 12:20:38.627 7835-7915/camera.test.com.networkdata E/TAG: onNext:::::::::::2::intervalRange:::::::::::::Thread[RxComputationThreadPool-1,5,main]
2019-08-02 12:20:39.626 7835-7915/camera.test.com.networkdata E/TAG: onNext:::::::::::3::intervalRange:::::::::::::Thread[RxComputationThreadPool-1,5,main]
2019-08-02 12:20:40.627 7835-7915/camera.test.com.networkdata E/TAG: onNext:::::::::::4::intervalRange:::::::::::::Thread[RxComputationThreadPool-1,5,main]
2019-08-02 12:20:41.626 7835-7915/camera.test.com.networkdata E/TAG: onNext:::::::::::5::intervalRange:::::::::::::Thread[RxComputationThreadPool-1,5,main]
4、 RxJava转换操作符使用介绍
操作符 | 介绍 |
---|---|
map | 对被观察者发送事件的改变 |
flatMap | 对被观察者发送事件的改变,将原来被观察者事件进行拆分与新事件合并,并返回一个新的被观察者 |
concatMap | 同上,最终事件顺序和未转变之前顺序一样 |
buffer | 将事件进行缓存 |
4. 1 、转换操作 map
Observable.just(1, 2, 3, 4, 5).map(new Function<Integer, String>() {
@Override
public String apply(Integer integer) throws Exception {
return "事件传递修改参数" + integer + "为字符串";
}
}).subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
Log.e("TAG", "accept:::::::::::::" + s);
}
});
通过llog看,每个事件都被处理过了,有之前的int类型变成了字符串类型
2019-08-02 13:38:30.200 9659-9659/camera.test.com.networkdata E/TAG: accept:::::::::::::事件传递修改参数1为字符串
2019-08-02 13:38:30.200 9659-9659/camera.test.com.networkdata E/TAG: accept:::::::::::::事件传递修改参数2为字符串
2019-08-02 13:38:30.200 9659-9659/camera.test.com.networkdata E/TAG: accept:::::::::::::事件传递修改参数3为字符串
2019-08-02 13:38:30.200 9659-9659/camera.test.com.networkdata E/TAG: accept:::::::::::::事件传递修改参数4为字符串
2019-08-02 13:38:30.200 9659-9659/camera.test.com.networkdata E/TAG: accept:::::::::::::事件传递修改参数5为字符串
4. 2 、转换操作 flatMap
Integer arr[] = {1, 2, 3, 4};
Observable.fromArray(arr).flatMap(new Function<Integer, ObservableSource<String>>() {
@Override
public ObservableSource<String> apply(Integer integer) throws Exception {
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < 3; i++) {
list.add("A" + integer + ";;;;" + i);
}
return Observable.fromIterable(list);
}
}).subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
Log.e("TAG", "flatMap::::::::::" + s);
}
});
通过log可以看出,原事件一一拆分和合并成新的事件序列。并进行传递,此处的新的事件序列和原时间序列不一定一致
2019-08-02 13:42:15.206 10168-10168/camera.test.com.networkdata E/TAG: flatMap::::::::::A1;;;;0
2019-08-02 13:42:15.206 10168-10168/camera.test.com.networkdata E/TAG: flatMap::::::::::A1;;;;1
2019-08-02 13:42:15.206 10168-10168/camera.test.com.networkdata E/TAG: flatMap::::::::::A2;;;;0
2019-08-02 13:42:15.206 10168-10168/camera.test.com.networkdata E/TAG: flatMap::::::::::A2;;;;1
2019-08-02 13:42:15.206 10168-10168/camera.test.com.networkdata E/TAG: flatMap::::::::::A3;;;;0
2019-08-02 13:42:15.206 10168-10168/camera.test.com.networkdata E/TAG: flatMap::::::::::A3;;;;1
2019-08-02 13:42:15.207 10168-10168/camera.test.com.networkdata E/TAG: flatMap::::::::::A4;;;;0
2019-08-02 13:42:15.207 10168-10168/camera.test.com.networkdata E/TAG: flatMap::::::::::A4;;;;1
4. 3 、转换操作 concatMap
Integer arr[] = {1, 2, 3, 4};
Observable.fromArray(arr).concatMap(new Function<Integer, ObservableSource<String>>() {
@Override
public ObservableSource<String> apply(Integer integer) throws Exception {
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < 3; i++) {
list.add("A" + integer + "::::::" + i);
}
return Observable.fromIterable(list);
// return Observable.just("A"+integer,"B"+integer);
}
}).subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
Log.e("TAG", "flatMap::::::::::" + s);
}
});
通过log得知,功能和flatMap类似,只是形成新的事件序列和原事件序列一致
2019-08-02 13:42:15.206 10168-10168/camera.test.com.networkdata E/TAG: flatMap::::::::::A1;;;;0
2019-08-02 13:42:15.206 10168-10168/camera.test.com.networkdata E/TAG: flatMap::::::::::A1;;;;1
2019-08-02 13:42:15.206 10168-10168/camera.test.com.networkdata E/TAG: flatMap::::::::::A2;;;;0
2019-08-02 13:42:15.206 10168-10168/camera.test.com.networkdata E/TAG: flatMap::::::::::A2;;;;1
2019-08-02 13:42:15.206 10168-10168/camera.test.com.networkdata E/TAG: flatMap::::::::::A3;;;;0
2019-08-02 13:42:15.206 10168-10168/camera.test.com.networkdata E/TAG: flatMap::::::::::A3;;;;1
2019-08-02 13:42:15.207 10168-10168/camera.test.com.networkdata E/TAG: flatMap::::::::::A4;;;;0
2019-08-02 13:42:15.207 10168-10168/camera.test.com.networkdata E/TAG: flatMap::::::::::A4;;;;1
4. 3 、缓存操作 buffer
参数一:缓存个数
参数二:缓存的步长
Integer arr[] = {1, 2, 3, 4};
Observable.fromArray(arr).buffer(4, 1).subscribe(new Observer<List<Integer>>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(List<Integer> value) {
for (Integer num : value) {
Log.e("TAG", "value:::::::" + num);
}
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
2019-08-02 14:16:39.405 11701-11701/camera.test.com.networkdata E/TAG: value:::::::1
2019-08-02 14:16:39.405 11701-11701/camera.test.com.networkdata E/TAG: value:::::::2
2019-08-02 14:16:39.405 11701-11701/camera.test.com.networkdata E/TAG: value:::::::3
2019-08-02 14:16:39.405 11701-11701/camera.test.com.networkdata E/TAG: value:::::::4
2019-08-02 14:16:39.406 11701-11701/camera.test.com.networkdata E/TAG: value:::::::2
2019-08-02 14:16:39.406 11701-11701/camera.test.com.networkdata E/TAG: value:::::::3
2019-08-02 14:16:39.406 11701-11701/camera.test.com.networkdata E/TAG: value:::::::4
2019-08-02 14:16:39.406 11701-11701/camera.test.com.networkdata E/TAG: value:::::::3
2019-08-02 14:16:39.406 11701-11701/camera.test.com.networkdata E/TAG: value:::::::4
5 、合并操作
操作符 | 简介 |
---|---|
concat / concatArray | 将多个被观察者 进行合并,发送事件的顺序按照 被观察者 的顺序发送事件 |
merge/ mergeArray | 将多个被观察者 进行合并,发送事件的顺序按时间线并行 执行 |
concatArrayDelayError/ mergeDelayError | onError事件 推迟到,其他被观察者发送完事件再执行 |
zip | 合并 多个被观察者(Observable)发送的事件,生成一个新的事件序列(即组合过后的事件序列),并最终发送 比如 Observable.just(1, 2, 3, 4, 5) 和Observable.fromArray("A", "B", "C", "D") 事件 1对应A,2对应B,以此类推,最终5没有结合者,观察者是不会被观察者接收 |
combineLatest | 将第一个被观察者的最后一个事件和第二个被观察的事件分别合并,并返回一个新的被观察者(Observable) |
combineLatest | 将第一个被观察者的最后一个事件和第二个被观察的事件分别合并,并返回一个新的被观察者(Observable) |
reduce | 将一个被观察者中的多个事件,进行操作,最终合并成一个事件(属于一个被观察者中内部事件的合并) |
collect | 将被观察者Observable发送的数据事件收集到一个数据结构里 (属于一个被观察者中内部事件的合并) |
startWith / startWithArray | 在一个被观察者发送事件前,追加发送一些数据 / 一个新的被观察者 |
5 .1 、合并操作 concat / concatArray
@contact
只能合并<=4个
Observable.concat(
Observable.intervalRange(1, 2, 3, 1, TimeUnit.SECONDS),
Observable.just(9l, 10l),
Observable.just(11l)).subscribe(new Consumer<Long>() {
@Override
public void accept(Long integer) throws Exception {
Log.e("TAG", "concat=============" + integer);
}
});
@concatArray
Observable.concatArray(Observable.just(1, 2, 3),
Observable.just(4, 5, 6),
Observable.just(7, 8, 9),
Observable.just(10, 11, 12),
Observable.just(13, 14, 15)).subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
Log.e("TAG", "concat=============" + integer);
}
});
@contact
2019-08-02 14:50:33.056 12590-13444/camera.test.com.networkdata E/TAG: concat=============1
2019-08-02 14:50:34.058 12590-13444/camera.test.com.networkdata E/TAG: concat=============2
2019-08-02 14:50:34.059 12590-13444/camera.test.com.networkdata E/TAG: concat=============9
2019-08-02 14:50:34.060 12590-13444/camera.test.com.networkdata E/TAG: concat=============10
2019-08-02 14:50:34.060 12590-13444/camera.test.com.networkdata E/TAG: concat=============11
@concatArray
2019-08-02 14:51:25.573 12590-12590/camera.test.com.networkdata E/TAG: concat=============1
2019-08-02 14:51:25.573 12590-12590/camera.test.com.networkdata E/TAG: concat=============2
2019-08-02 14:51:25.573 12590-12590/camera.test.com.networkdata E/TAG: concat=============3
2019-08-02 14:51:25.573 12590-12590/camera.test.com.networkdata E/TAG: concat=============4
2019-08-02 14:51:25.574 12590-12590/camera.test.com.networkdata E/TAG: concat=============5
2019-08-02 14:51:25.574 12590-12590/camera.test.com.networkdata E/TAG: concat=============6
2019-08-02 14:51:25.574 12590-12590/camera.test.com.networkdata E/TAG: concat=============7
2019-08-02 14:51:25.574 12590-12590/camera.test.com.networkdata E/TAG: concat=============8
2019-08-02 14:51:25.574 12590-12590/camera.test.com.networkdata E/TAG: concat=============9
2019-08-02 14:51:25.574 12590-12590/camera.test.com.networkdata E/TAG: concat=============10
2019-08-02 14:51:25.574 12590-12590/camera.test.com.networkdata E/TAG: concat=============11
2019-08-02 14:51:25.574 12590-12590/camera.test.com.networkdata E/TAG: concat=============12
2019-08-02 14:51:25.574 12590-12590/camera.test.com.networkdata E/TAG: concat=============13
2019-08-02 14:51:25.574 12590-12590/camera.test.com.networkdata E/TAG: concat=============14
2019-08-02 14:51:25.574 12590-12590/camera.test.com.networkdata E/TAG: concat=============15
5 .2 、合并操作 merge/ mergeArray
和 concat / concatArray 功能类似,不同参考上边总结
Observable.merge(
//从1开始生成2个数字,首次延迟3秒,之后延迟1秒
Observable.intervalRange(1, 2, 3, 1, TimeUnit.SECONDS),
Observable.just(5l, 6l, 7l, 8l),
Observable.just(9l, 10l),
Observable.just(11l)).subscribe(new Consumer<Long>() {
@Override
public void accept(Long integer) throws Exception {
Log.e("TAG", "concat=============" + integer);
}
});
2019-08-02 14:54:05.238 12590-12590/camera.test.com.networkdata E/TAG: concat=============5
2019-08-02 14:54:05.238 12590-12590/camera.test.com.networkdata E/TAG: concat=============6
2019-08-02 14:54:05.238 12590-12590/camera.test.com.networkdata E/TAG: concat=============7
2019-08-02 14:54:05.238 12590-12590/camera.test.com.networkdata E/TAG: concat=============8
2019-08-02 14:54:05.239 12590-12590/camera.test.com.networkdata E/TAG: concat=============9
2019-08-02 14:54:05.239 12590-12590/camera.test.com.networkdata E/TAG: concat=============10
2019-08-02 14:54:05.239 12590-12590/camera.test.com.networkdata E/TAG: concat=============11
2019-08-02 14:54:07.347 1486-1486/? W/KeyguardUpdateMonitor: ChargingSpeed Wattage: 6860000 ST: 5000000 --> 7500000
2019-08-02 14:54:08.237 12590-13581/camera.test.com.networkdata E/TAG: concat=============1
2019-08-02 14:54:09.238 12590-13581/camera.test.com.networkdata E/TAG: concat=============2
5 .3 、合并操作 concatArrayDelayError/mergeDelayError
concatArrayDelayError/mergeDelayError 功能类似就演示一个 concatArrayDelayError
Observable.concatArrayDelayError(Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> e) throws Exception {
e.onNext(1);
e.onNext(2);
e.onNext(3);
e.onNext(4);
e.onError(new MyException("测试异常"));
}
}), Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> e) throws Exception {
e.onNext(6);
}
})).subscribe(new Observer<Integer>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Integer value) {
Log.e("TAG", "concat=============" + value);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
如果用contact操作符号,就不会接收到事件 6
2019-08-02 14:57:17.524 12590-12590/camera.test.com.networkdata E/TAG: concat=============1
2019-08-02 14:57:17.524 12590-12590/camera.test.com.networkdata E/TAG: concat=============2
2019-08-02 14:57:17.524 12590-12590/camera.test.com.networkdata E/TAG: concat=============3
2019-08-02 14:57:17.524 12590-12590/camera.test.com.networkdata E/TAG: concat=============4
2019-08-02 14:57:17.525 12590-12590/camera.test.com.networkdata E/TAG: concat=============6
5 .4 、合并操作 zip
Observable.zip(Observable.just(1, 2, 3, 4, 5), Observable.fromArray("A", "B", "C", "D"), new BiFunction<Integer, String, String>() {
@Override
public String apply(Integer integer, String s) throws Exception {
return integer + "::" + s;
}
}).subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
Log.e("TAG", "======" + s);
}
});
2019-08-02 14:59:34.689 12590-12590/camera.test.com.networkdata E/TAG: ======1::A
2019-08-02 14:59:34.689 12590-12590/camera.test.com.networkdata E/TAG: ======2::B
2019-08-02 14:59:34.690 12590-12590/camera.test.com.networkdata E/TAG: ======3::C
2019-08-02 14:59:34.690 12590-12590/camera.test.com.networkdata E/TAG: ======4::D
5 .5 、合并操作 combineLatest
Observable<Integer> integerObservable2 = Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> e) throws Exception {
e.onNext(4);
e.onNext(5);
e.onNext(6);
}
});
Observable.combineLatest(integerObservable2,Observable.just(1, 2, 3), new BiFunction<Integer, Integer, Integer>() {
@Override
public Integer apply(Integer integer, Integer integer2) throws Exception {
return integer+integer2;
}
}).subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
Log.e("TAG","合并结果是:"+integer);
}
});
2019-08-02 15:00:54.334 12590-12590/camera.test.com.networkdata E/TAG: 合并结果是:7
2019-08-02 15:00:54.334 12590-12590/camera.test.com.networkdata E/TAG: 合并结果是:8
2019-08-02 15:00:54.335 12590-12590/camera.test.com.networkdata E/TAG: 合并结果是:9
5 .6 、合并操作 reduce
代码含义:求 1+2+3+4的和
Observable.just(1,2,3,4).reduce(new BiFunction<Integer, Integer, Integer>() {
@Override
public Integer apply(Integer integer, Integer integer2) throws Exception {
Log.e("TAG", integer2+"apply: "+integer );
return integer+integer2;
}
}).subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
Log.e("TAG","============="+integer);
}
});
2019-08-02 15:03:23.933 12590-12590/camera.test.com.networkdata E/TAG: =============10
5 .7 、合并操作 collect
Observable.just(1,2,3,4).collect(
// 1. 创建数据结构(容器),用于收集被观察者发送的数据
new Callable<ArrayList<Integer>>() {
@Override
public ArrayList<Integer> call() throws Exception {
return new ArrayList<>();
}
// 2. 对发送的数据进行收集
}, new BiConsumer<ArrayList<Integer>, Integer>() {
@Override
public void accept(ArrayList<Integer> integers, Integer integer) throws Exception {
integers.add(integer+1);
}
}).subscribe(new Consumer<ArrayList<Integer>>() {
@Override
public void accept(ArrayList<Integer> integers) throws Exception {
Log.e("TAG","==========="+integers);
}
});
2019-08-02 15:05:20.191 12590-12590/camera.test.com.networkdata E/TAG: ===========[2, 3, 4, 5]
5 .8 、合并操作 startWith / startWithArray
startWith / startWithArray 功能类似
Observable.just(1,2,3).startWith(Observable.just(3,2,1)).subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
Log.e("TAG","============="+integer);
}
});
2019-08-02 15:26:48.740 12590-12590/camera.test.com.networkdata E/TAG: =============3
2019-08-02 15:26:48.741 12590-12590/camera.test.com.networkdata E/TAG: =============2
2019-08-02 15:26:48.741 12590-12590/camera.test.com.networkdata E/TAG: =============1
2019-08-02 15:26:48.741 12590-12590/camera.test.com.networkdata E/TAG: =============1
2019-08-02 15:26:48.741 12590-12590/camera.test.com.networkdata E/TAG: =============2
2019-08-02 15:26:48.741 12590-12590/camera.test.com.networkdata E/TAG: =============3
6 、功能操作符
操作符 | 介绍 |
---|---|
subscribe | 连接观察者 Observer 和被观察者 Observable 之间的纽带 |
subscribeOn() / observeOn() | 线程控制(也称为调度 / 切换) subscribeOn 指定被观察者(Observable)的线程,observeOn 指定观察者的线程(Observer ) |
delay | 延迟被观察者(Observable)延迟发送事件 |
6.1 、subscribe
subscribe 连接 Observable和observer
Observable.just(1,2,3).subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
Log.e("TAG","============="+integer);
}
});
6.2 、subscribeOn() / observeOn()
指定Observable和observer被观察者和观察者所在的线程首先看下Rx都提供哪些可以选择的线程
线程名称 | 线程含义 |
---|---|
Schedulers.immediate() | 当前线程 = 不指定线程 |
AndroidSchedulers.mainThread() | Android主线程 UI线程 |
Schedulers.newThread() | 指定普通的新线程 |
Schedulers.io() | io操作线程 |
Schedulers.computation() | CPU计算操作线程 |
Observable.just(1,2,3)
.subscribeOn(Schedulers.io()) //指定被观察者在IO线程
.observeOn(AndroidSchedulers.mainThread()); // 指定观察者在UI线程
6.3 、delay
延迟发送事件
Observable.just(1,3,4).delay(3,TimeUnit.SECONDS).subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
Log.e("TAG", "延迟3秒接收到事件::::"+integer);
}
});
7 、do操作符
所谓的do操作符号,可以理解为事件发送的监听
操作符号 | 含义 |
---|---|
doOnSubscribe | 当Observable和Observer关联的时候调用 |
doOnEach | 当Observable每发送1次数据事件就会调用1次 |
doOnNext | 执行Next事件前调用 |
doAfterNext | 执行Next事件后调用 |
doOnComplete | Observable正常发送事件完毕后调用 |
doOnError | Observable发送错误事件时调用 |
doOnSubscribe | 观察者订阅时调用 |
doAfterTerminate | Observable发送事件完毕后调用,无论正常发送完毕 / 异常终止 |
doFinally | 最后执行 |
Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> e) throws Exception {
e.onNext(1);
e.onError(new MyException("我出错了"));
e.onComplete();
}
}).doOnEach(new Consumer<Notification<Integer>>() {
@Override
public void accept(Notification<Integer> integerNotification) throws Exception {
Log.e("TAG", "doOnEach::::"+integerNotification.getValue());
}
}).doOnNext(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
Log.e("TAG", "doOnNext:::::::::::"+integer);
}
}).doAfterNext(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
Log.e("TAG", "doAfterNext:::::::::::"+integer);
}
}).doOnComplete(new Action() {
@Override
public void run() throws Exception {
Log.e("TAG", "doOnComplete::::::");
}
}).doOnError(new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
Log.e("TAG", "doOnError::::::::::"+throwable.getMessage());
}
}).doAfterTerminate(new Action() {
@Override
public void run() throws Exception {
Log.e("TAG", "doAfterTerminate::::::::::");
}
}).doFinally(new Action() {
@Override
public void run() throws Exception {
Log.e("TAG", "doFinally::::::::::");
}
}).doOnDispose(new Action() {
@Override
public void run() throws Exception {
Log.e("TAG", "doOnDispose:::::::::::::");
}
}).doOnSubscribe(new Consumer<Disposable>() {
@Override
public void accept(Disposable disposable) throws Exception {
Log.e("TAG", "doOnSubscribe:::::::::::::");
}
}).subscribe(new Observer<Integer>() {
@Override
public void onSubscribe(Disposable d) {
Log.e("TAG", "onSubscribe================:");
}
@Override
public void onNext(Integer value) {
Log.e("TAG", "onSubscribe================="+value);
}
@Override
public void onError(Throwable e) {
Log.e("TAG", "onError================="+e);
}
@Override
public void onComplete() {
Log.e("TAG", "onComplete=================");
}
});
}
public void RX2collect(View view){
Observable.just(1,2,3,4).collect(new Callable<ArrayList<Integer>>() {
@Override
public ArrayList<Integer> call() throws Exception {
return new ArrayList<>();
}
}, new BiConsumer<ArrayList<Integer>, Integer>() {
@Override
public void accept(ArrayList<Integer> integers, Integer integer) throws Exception {
integers.add(integer+1);
}
}).subscribe(new Consumer<ArrayList<Integer>>() {
@Override
public void accept(ArrayList<Integer> integers) throws Exception {
Log.e("TAG","==========="+integers);
}
});
}
public void RX2reduce(View view){
Observable.just(1,2,3,4).reduce(new BiFunction<Integer, Integer, Integer>() {
@Override
public Integer apply(Integer integer, Integer integer2) throws Exception {
Log.e("TAG", integer2+"apply: "+integer );
return integer+integer2;
}
}).subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
Log.e("TAG","============="+integer);
}
});
log 发现在发送Error事件的时候,只调用了doOnEach方法和doOnError方法回调,doAfterTerminate在doFinally之后
2019-08-02 16:09:03.268 23060-23060/camera.test.com.networkdata E/TAG: doOnSubscribe:::::::::::::
2019-08-02 16:09:03.268 23060-23060/camera.test.com.networkdata E/TAG: onSubscribe================:
2019-08-02 16:09:03.268 23060-23060/camera.test.com.networkdata E/TAG: doOnEach::::1
2019-08-02 16:09:03.268 23060-23060/camera.test.com.networkdata E/TAG: doOnNext:::::::::::1
2019-08-02 16:09:03.268 23060-23060/camera.test.com.networkdata E/TAG: onSubscribe=================1
2019-08-02 16:09:03.268 23060-23060/camera.test.com.networkdata E/TAG: doAfterNext:::::::::::1
2019-08-02 16:09:03.269 23060-23060/camera.test.com.networkdata E/TAG: doOnEach::::null
2019-08-02 16:09:03.269 23060-23060/camera.test.com.networkdata E/TAG: doOnError::::::::::我出错了
2019-08-02 16:09:03.269 23060-23060/camera.test.com.networkdata E/TAG: onError=================camera.test.com.networkdata.exception.MyException: 我出错了
2019-08-02 16:09:03.269 23060-23060/camera.test.com.networkdata E/TAG: doFinally::::::::::
2019-08-02 16:09:03.269 23060-23060/camera.test.com.networkdata E/TAG: doAfterTerminate::::::::::
8 、过滤操作符
所谓的过滤操作符就是 有条件的发送事件。
操作符 | 含义 |
---|---|
filter | 根据条件发送事件 |
ofType | 根据类型过滤事件 |
8.1、 filter 过滤
Observable.just(1,2,3,4,5,6).filter(new Predicate<Integer>() {
@Override
public boolean test(Integer integer) throws Exception {
//reutrn true 表示发送,false表示不发送
return integer%3==0;
}
}).subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
Log.e("TAG", "=============="+integer);
}
});
是发送能整除3的事件
2019-08-02 16:53:29.224 6126-6126/camera.test.com.networkdata E/TAG: ==============3
2019-08-02 16:53:29.224 6126-6126/camera.test.com.networkdata E/TAG: ==============6
8.2、 ofType 类型过滤
bservable.just("A", 1, 0.3f, "B", 4L).ofType(String.class).subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
Log.e("TAG", "========"+s);
}
});
只发送字符串类型的事件
2019-08-02 17:02:34.079 6949-6949/camera.test.com.networkdata E/TAG: ========A
2019-08-02 17:02:34.079 6949-6949/camera.test.com.networkdata E/TAG: ========B
8.3、 skip() / skipLast()过滤
skip 从事件的开始位置 选择跳过X个事件
,skipLast 从事件结束位置选择跳过X个事件
Observable.just(1,2,3,4,5,6,7).skip(2).skipLast(2).subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
Log.e("TAG", "======="+integer);
}
});
从事件(1,2,3,4,5,6,7) 截取3,4,5
2019-08-02 17:07:56.711 7423-7423/camera.test.com.networkdata E/TAG: =======3
2019-08-02 17:07:56.711 7423-7423/camera.test.com.networkdata E/TAG: =======4
2019-08-02 17:07:56.711 7423-7423/camera.test.com.networkdata E/TAG: =======5
8.4、 distinct() / distinctUntilChanged()过滤
distinct
过滤重复事件,distinctUntilChanged
过滤连续的重复事件
Observable.just(1,1,2,1,1,1,1,1).distinct().subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer s) throws Exception {
Log.e("TAG", "============"+s);
}
});
distinct过滤去重复结果
2019-08-02 17:19:34.529 8771-8771/camera.test.com.networkdata E/TAG: ============1
2019-08-02 17:19:34.529 8771-8771/camera.test.com.networkdata E/TAG: ============2
Observable.just(1,1,2,1,1,1,1,1).distinctUntilChanged().subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer s) throws Exception {
Log.e("TAG", "============"+s);
}
});
distinctUntilChanged 过滤去重复结果
2019-08-02 17:19:34.528 8771-8771/camera.test.com.networkdata E/TAG: ============1
2019-08-02 17:19:34.529 8771-8771/camera.test.com.networkdata E/TAG: ============2
2019-08-02 17:19:34.529 8771-8771/camera.test.com.networkdata E/TAG: ============1
对比distinctUntilChanged和distinct 只是把相邻事件间中的重复事件过滤。
8.5、 take() & takeLast() 过滤
通过设置指定的事件数量,仅发送特定数量的事件 take是从前开始制定个数,takeLast是从后边开始指定。
Observable.just(1,2,3,4,5,6,7).take(3).takeLast(1).subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
Log.e("TAG", "============"+integer);
}
});
先从事件开始位置取出3个事件(1,2,3)再从事件后边开始取出一个事件(3)
2019-08-02 17:28:02.140 9533-9533/camera.test.com.networkdata E/TAG: ============3
还可以过滤某个时间段内的数据
Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> e) throws Exception {
e.onNext(0);
Thread.sleep(1000);
e.onNext(1);
Thread.sleep(500);
e.onNext(2);
Thread.sleep(2000);
e.onNext(3);
}
}).take(1,TimeUnit.SECONDS).observeOn(Schedulers.newThread()).subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
Log.e("TAG", "=============="+integer);
}
});
2019-08-02 17:37:23.830 10721-10773/camera.test.com.networkdata E/TAG: ==============0
8.6、 tthrottleFirst()/ throttleLast() 过滤
过滤某个时间段内的第一个/最后一个事件,直到事件发送完
Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> e) throws Exception {
e.onNext(0);
Thread.sleep(1000);
e.onNext(1);
Thread.sleep(500);
e.onNext(2);
Thread.sleep(2000);
e.onNext(3);
}
}).throttleFirst(1,TimeUnit.SECONDS).observeOn(Schedulers.newThread()).subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
Log.e("TAG", "=============="+integer);
}
});
Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> e) throws Exception {
e.onNext(0);
Thread.sleep(1000);
e.onNext(1);
Thread.sleep(500);
e.onNext(2);
Thread.sleep(2000);
e.onNext(3);
}
}).throttleLast(1,TimeUnit.SECONDS).observeOn(Schedulers.newThread()).subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
Log.e("TAG", ":::::::::::::"+integer);
}
});
2019-08-02 17:53:26.301 11504-11557/camera.test.com.networkdata E/TAG: ==============0
2019-08-02 17:53:27.302 11504-11557/camera.test.com.networkdata E/TAG: ==============1
2019-08-02 17:53:29.806 11504-11557/camera.test.com.networkdata E/TAG: ==============3
2019-08-02 17:53:30.815 11504-11560/camera.test.com.networkdata E/TAG: :::::::::::::0
2019-08-02 17:53:31.812 11504-11560/camera.test.com.networkdata E/TAG: :::::::::::::2
2019-08-02 17:53:33.812 11504-11560/camera.test.com.networkdata E/TAG: :::::::::::::3
8.7、 throttleWithTimeout () / debounce()过滤
发送数据事件时,若2次发送事件的间隔<指定时间,就会丢弃前一次的数据,直到指定时间内都没有新数据发射时才会发送后一次的数据
Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> e) throws Exception {
e.onNext(0);
Thread.sleep(1000);
e.onNext(1);
Thread.sleep(500);
e.onNext(2);
Thread.sleep(2000);
e.onNext(3);
}
}).throttleWithTimeout(1,TimeUnit.SECONDS).observeOn(Schedulers.newThread()).subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
Log.e("TAG", "=============="+integer);
}
});
在1秒中内只取最后的一个事件数据
2019-08-02 18:04:47.429 11874-11949/camera.test.com.networkdata E/TAG: ==============0
2019-08-02 18:04:48.934 11874-11949/camera.test.com.networkdata E/TAG: ==============2
2019-08-02 18:04:50.934 11874-11949/camera.test.com.networkdata E/TAG: ==============3
8.8、 firstElement() / lastElement() 过滤
操作符 | 含义 |
---|---|
firstElement | 过滤第一个事件 |
lastElement | 过滤第最后一个事件 |
elementAt | 按照索引过滤特定事件 |
9、 Boolean 操作符
(@1)==判断发送的每项数据是否都满足 设置的函数条件
操作符 | 含义 |
---|---|
all | (@1)(返回值Boolean类型) |
takeWhile | (@1)若发送的数据满足该条件,则发送该项数据;否则不发送 |
skipWhile | (@1) 直到该判断条件 = false时,才开始发送Observable的数据 |
takeUntil | (@1)执行到某个条件时,停止发送事件 |
skipUntil | (@1)等到 skipUntil() 传入的Observable开始发送数据,(原始)第1个Observable的数据才开始发送数据 |
sequenceEqual | 判定两个Observables需要发送的数据是否相同 |
contains | 判断发送的数据中是否包含指定数据 |
isEmpty | 判断发送的数据是否为空 |
amb | 当需要发送多个 Observable时,只发送 先发送数据的Observable的数据,而其余 Observable则被丢弃 |
defaultIfEmpty | 在不发送任何有效事件( Next事件)、仅发送了 Complete 事件的前提下,发送一个默认值 |
9.1、 all 操作符
判断所有事件是否满足 特定条件
Observable.just(1,2,3,4,5,6).all(new Predicate<Integer>() {
@Override
public boolean test(Integer integer) throws Exception {
Log.e("TAG","=========="+integer);
return integer<3;
}
}).subscribe(new Consumer<Boolean>() {
@Override
public void accept(Boolean aBoolean) throws Exception {
Log.e("TAG",":::::::::::::::"+aBoolean);
}
});
判断所有事件是否<3
08-04 13:03:34.765 1494-1494/camera.test.com.networkdata E/TAG: ==========1
08-04 13:03:34.765 1494-1494/camera.test.com.networkdata E/TAG: ==========2
08-04 13:03:34.765 1494-1494/camera.test.com.networkdata E/TAG: ==========3
08-04 13:03:34.765 1494-1494/camera.test.com.networkdata E/TAG: :::::::::::::::false
9.2、 takeWhile 操作符
Observable.just(1,2,3,4,5,6,2,3).takeWhile(new Predicate<Integer>() {
@Override
public boolean test(Integer integer) throws Exception {
return integer<4;
}
}).subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
Log.e("TAG",":::::::::::::"+integer);
}
});
从第一个事件一次判断,如果满足条件就发送当前事件,直到不满足就终止事件发送
08-04 13:15:18.205 1749-1749/camera.test.com.networkdata E/TAG: :::::::::::::1
08-04 13:15:18.205 1749-1749/camera.test.com.networkdata E/TAG: :::::::::::::2
08-04 13:15:18.205 1749-1749/camera.test.com.networkdata E/TAG: :::::::::::::3
9.3、 skipWhile操作符
Observable.just(1,2,3,4,5,6,2,2).skipWhile(new Predicate<Integer>() {
@Override
public boolean test(Integer integer) throws Exception {
return integer<4;
}
}).subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
Log.e("TAG",":::::::::::::"+integer);
}
});
判断每个事件是否满足条件,当不满足条件时候发送事件,和takeWhile 相反
08-04 13:25:27.475 1902-1902/camera.test.com.networkdata E/TAG: :::::::::::::4
08-04 13:25:27.475 1902-1902/camera.test.com.networkdata E/TAG: :::::::::::::5
08-04 13:25:27.475 1902-1902/camera.test.com.networkdata E/TAG: :::::::::::::6
08-04 13:25:27.475 1902-1902/camera.test.com.networkdata E/TAG: :::::::::::::2
08-04 13:25:27.475 1902-1902/camera.test.com.networkdata E/TAG: :::::::::::::2
9.4、 takeUntil 操作符
Observable.just(1,2,3,4,5,6,2,2).takeUntil(new Predicate<Integer>() {
@Override
public boolean test(Integer integer) throws Exception {
return integer>3;
}
}).subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
Log.e("TAG",":::::::::::::"+integer);
}
});
takeUntil 当事件<3的时候停止发送数据,log可以看出4>3,也发送事件了,由此可以看出,是先发送一次数据再进行条件判断
08-04 13:29:40.395 2048-2048/camera.test.com.networkdata E/TAG: :::::::::::::1
08-04 13:29:40.395 2048-2048/camera.test.com.networkdata E/TAG: :::::::::::::2
08-04 13:29:40.395 2048-2048/camera.test.com.networkdata E/TAG: :::::::::::::3
08-04 13:29:40.395 2048-2048/camera.test.com.networkdata E/TAG: :::::::::::::4
9.4、 skipUntil 操作符
从1开始发送10个数据,第一次延迟1秒发送数据,之后有也延迟1s发送数据
Observable.intervalRange(1,10,1,1,TimeUnit.SECONDS).
从0开始发送2个数据,第一次延迟2s发送数据,之后延迟1s发送数据
skipUntil(Observable.intervalRange(0,2,2,1,TimeUnit.SECONDS)).subscribe(new Consumer<Long>() {
@Override
public void accept(Long aLong) throws Exception {
Log.e("TAG",":::::::::::"+aLong);
}
});
当skipUntil 中的observable 被观察者发送数据的时候,原始的observable才开始发送数据,自己理解 默认原始的observable 已经开始发送数据,但是观察者接收不到,直到skipUntil 中的observable 被观察者发送数据的时候,才算真正的发送数据
08-04 13:43:05.475 2414-2435/camera.test.com.networkdata E/TAG: :::::::::::2
08-04 13:43:06.475 2414-2435/camera.test.com.networkdata E/TAG: :::::::::::3
08-04 13:43:07.475 2414-2435/camera.test.com.networkdata E/TAG: :::::::::::4
08-04 13:43:08.475 2414-2435/camera.test.com.networkdata E/TAG: :::::::::::5
08-04 13:43:09.475 2414-2435/camera.test.com.networkdata E/TAG: :::::::::::6
08-04 13:43:10.475 2414-2435/camera.test.com.networkdata E/TAG: :::::::::::7
08-04 13:43:11.475 2414-2435/camera.test.com.networkdata E/TAG: :::::::::::8
08-04 13:43:12.475 2414-2435/camera.test.com.networkdata E/TAG: :::::::::::9
08-04 13:43:13.475 2414-2435/camera.test.com.networkdata E/TAG: :::::::::::10
9.5、 sequenceEqual操作符
Observable.sequenceEqual(Observable.just(1,2),Observable.just(1)).subscribe(new Consumer<Boolean>() {
@Override
public void accept(Boolean aBoolean) throws Exception {
Log.e("TAG",":::::::::"+aBoolean);
}
});
sequenceEqual 判断两个被观察者是否发送相同事件
08-04 13:52:17.315 2493-2493/camera.test.com.networkdata E/TAG: :::::::::false
9.6、 contains 操作符
Observable.just(1,2,3,4).contains(1).subscribe(new Consumer<Boolean>() {
@Override
public void accept(Boolean aBoolean) throws Exception {
Log.e("TAG","contains::::::::::::"+aBoolean);
}
});
发送的事件中是否包含 某个事件
08-04 14:04:19.565 2746-2746/camera.test.com.networkdata E/TAG: contains::::::::::::true
9.7、 isEmpty 操作符
Observable.just(1,2,3).isEmpty().subscribe(new Consumer<Boolean>() {
@Override
public void accept(Boolean aBoolean) throws Exception {
Log.e("TAG","isEmpty::::::::::::"+aBoolean);
}
});
是否发送的是事件是否是空
08-04 14:04:19.565 2746-2746/camera.test.com.networkdata E/TAG: isEmpty::::::::::::false
9.8、 amb 操作符
ArrayList<Observable<Long>> arrayList=new ArrayList<>();
arrayList.add(Observable.timer(2,TimeUnit.SECONDS));
arrayList.add(Observable.just(1L,2L,3L));
Observable.amb(arrayList).subscribe(new Consumer<Long>() {
@Override
public void accept(Long o) throws Exception {
Log.e("TAG","amb::::::::::::"+o);
}
});
如果有两个被观察者,那个被观察者先发送事件,剩下的被观察者就不发送事件
08-04 14:04:19.565 2746-2746/camera.test.com.networkdata E/TAG: amb::::::::::::1
08-04 14:04:19.565 2746-2746/camera.test.com.networkdata E/TAG: amb::::::::::::2
08-04 14:04:19.565 2746-2746/camera.test.com.networkdata E/TAG: amb::::::::::::3
9.9、 defaultIfEmpty 操作符
bservable.empty().defaultIfEmpty("A").subscribe(new Consumer<Object>() {
@Override
public void accept(Object s) throws Exception {
Log.e("TAG","defaultIfEmpty:::::::::"+s);
}
});
如果没有发送onNext事件,就发送一个默认的onNext事件
08-04 14:04:19.565 2746-2746/camera.test.com.networkdata E/TAG: defaultIfEmpty:::::::::A
以上所有是Rx的基本操作和使用,以及基本的操作符的使用和使用方法。