1、作用
- 通过设置函数,判断被观察者(
Observable
)发送的事件是否符合条件
2、类型
3、详解
3.1 all()----- 见rxdocs.pdf第192页
作用:判断发送的每项数据是否都满足 设置的函数条件
public static void all() { Observable.just(1, 2, 3, 4, 5, 10) .all(new Predicate<Integer>() { @Override public boolean test(Integer integer) throws Exception { return integer >= 10; } }) .subscribe(new SingleObserver<Boolean>() { @Override public void onSubscribe(Disposable d) { Log.d(TAG, "onSubscribe"); } @Override public void onSuccess(Boolean value) { Log.d(TAG, "onSuccess: value = " + value); } @Override public void onError(Throwable e) { Log.d(TAG, "onError: " + e.toString()); } }); }
输出:
08-09 19:56:20.782 4775 4775 D Operation: onSubscribe 08-09 19:56:20.782 4775 4775 D Operation: onSuccess: value = false
3.2 takeWhile()----- 见rxdocs.pdf第201页
作用:发射Observable发射的数据,直到一个指定的条件不成立
public static void takeWhile() { Observable.just(1, 2, 3, 4, 5, 10) .takeWhile(new Predicate<Integer>() { @Override public boolean test(Integer integer) throws Exception { return integer < 4; } }) .subscribe(new Observer<Integer>() { @Override public void onSubscribe(Disposable d) { Log.d(TAG, "onSubscribe"); } @Override public void onNext(Integer value) { Log.d(TAG, "onNext: value = " + value); } @Override public void onError(Throwable e) { Log.d(TAG, "onError: " + e.toString()); } @Override public void onComplete() { Log.d(TAG, "onComplete"); } }); }
输出:
08-09 20:02:26.093 5131 5131 D Operation: onSubscribe
08-09 20:02:26.093 5131 5131 D Operation: onNext: value = 1
08-09 20:02:26.093 5131 5131 D Operation: onNext: value = 2
08-09 20:02:26.093 5131 5131 D Operation: onNext: value = 3
08-09 20:02:26.093 5131 5131 D Operation: onComplete
3.3 skipWhile()----- 见rxdocs.pdf第198页
作用:丢弃Observable发射的数据,直到一个指定的条件不成立。即直到该判断条件 = false
时,才开始发送Observable
的数据。与takeWhile()相反。
public static void skipWhile() { Observable.just(1, 2, 3, 4, 5, 10) .skipWhile(new Predicate<Integer>() { @Override public boolean test(Integer integer) throws Exception { return integer < 4; } }).subscribe(new Observer<Integer>() { @Override public void onSubscribe(Disposable d) { Log.d(TAG, "onSubscribe"); } @Override public void onNext(Integer value) { Log.d(TAG, "onNext: value = " + value); } @Override public void onError(Throwable e) { Log.d(TAG, "onError: " + e.toString()); } @Override public void onComplete() { Log.d(TAG, "onComplete"); } }); }
输出:
08-12 11:04:21.003 12582 12582 D Operation: onSubscribe
08-12 11:04:21.003 12582 12582 D Operation: onNext: value = 4
08-12 11:04:21.003 12582 12582 D Operation: onNext: value = 5
08-12 11:04:21.003 12582 12582 D Operation: onNext: value = 10
08-12 11:04:21.003 12582 12582 D Operation: onComplete
3.4 takeUtil()----- 见rxdocs.pdf第199页
作用:执行到某个条件时,停止发送事件
public static void takeUtil() { Observable.just(1, 2, 3, 4, 5, 10) .takeUntil(new Predicate<Integer>() { @Override public boolean test(Integer integer) throws Exception { return integer > 4; } }).subscribe(new Observer<Integer>() { @Override public void onSubscribe(Disposable d) { Log.d(TAG, "onSubscribe"); } @Override public void onNext(Integer value) { Log.d(TAG, "onNext: value = " + value); } @Override public void onError(Throwable e) { Log.d(TAG, "onError: " + e.toString()); } @Override public void onComplete() { Log.d(TAG, "onComplete"); } }); }
输出:
08-12 11:10:31.067 12856 12856 D Operation: onSubscribe
08-12 11:10:31.067 12856 12856 D Operation: onNext: value = 1
08-12 11:10:31.067 12856 12856 D Operation: onNext: value = 2
08-12 11:10:31.067 12856 12856 D Operation: onNext: value = 3
08-12 11:10:31.067 12856 12856 D Operation: onNext: value = 4
08-12 11:10:31.067 12856 12856 D Operation: onNext: value = 5
08-12 11:10:31.067 12856 12856 D Operation: onComplete
3.5 skipUtil()----- 见rxdocs.pdf第198页
作用:丢弃原始Observable发射的数据,直到第二个Observable发射了一项数据
public static void skipUtil() { Observable.interval(1, TimeUnit.SECONDS) .skipUntil(Observable.timer(5, TimeUnit.SECONDS)) .subscribe(new Observer<Long>() { @Override public void onSubscribe(Disposable d) { Log.d(TAG, "onSubscribe"); } @Override public void onNext(Long value) { Log.d(TAG, "onNext: value = " + value); } @Override public void onError(Throwable e) { Log.d(TAG, "onError: " + e.toString()); } @Override public void onComplete() { Log.d(TAG, "onComplete"); } }); }
输出:
08-12 11:18:39.298 13159 13159 D Operation: onSubscribe
08-12 11:18:45.303 13159 13184 D Operation: onNext: value = 5
08-12 11:18:46.302 13159 13184 D Operation: onNext: value = 6
08-12 11:18:47.303 13159 13184 D Operation: onNext: value = 7
3.6 sequenceEqual()----- 见rxdocs.pdf第196页
作用:传递两个Observable给SequenceEqual
操作符,它会比较两个Observable的发射物,如果两个序列是相同的(相同的数据,相同的顺序,相同的终止状态),它就发射true,否则发射false。
public static void sequenceEqual() { Observable.sequenceEqual(Observable.just(1, 2, 3), Observable.just(1, 2, 3)) .subscribe(new SingleObserver<Boolean>() { @Override public void onSubscribe(Disposable d) { Log.d(TAG, "onSubscribe"); } @Override public void onSuccess(Boolean value) { Log.d(TAG, "onSuccess: value = " + value); } @Override public void onError(Throwable e) { Log.d(TAG, "onError: " + e.toString()); } }); }
输出:
08-12 11:24:11.229 13438 13438 D Operation: onSubscribe 08-12 11:24:11.230 13438 13438 D Operation: onSuccess: value = true
3.7 contains()----- 见rxdocs.pdf第194页
作用:判断发送的数据中是否包含指定数据
public static void contains() { Observable.just(1, 2, 3, 4) .contains(2) .subscribe(new SingleObserver<Boolean>() { @Override public void onSubscribe(Disposable d) { Log.d(TAG, "onSubscribe"); } @Override public void onSuccess(Boolean value) { Log.d(TAG, "onSuccess: value = " + value); } @Override public void onError(Throwable e) { Log.d(TAG, "onError: " + e.toString()); } }); }
输出:
08-12 11:28:58.716 13670 13670 D Operation: onSubscribe 08-12 11:28:58.716 13670 13670 D Operation: onSuccess: value = true
3.8 isEmpty()----- 见rxdocs.pdf第195页
作用:判断发送的数据是否为空
public static void isEmpty() { Observable.empty() .isEmpty() .subscribe(new SingleObserver<Boolean>() { @Override public void onSubscribe(Disposable d) { Log.d(TAG, "onSubscribe"); } @Override public void onSuccess(Boolean value) { Log.d(TAG, "onSuccess: value = " + value); } @Override public void onError(Throwable e) { Log.d(TAG, "onError: " + e.toString()); } }); }
输出:
08-12 11:32:22.533 13892 13892 D Operation: onSubscribe 08-12 11:32:22.533 13892 13892 D Operation: onSuccess: value = true
3.9 amb()----- 见rxdocs.pdf第193页
作用:当需要发送多个 Observable
时,只发送 先发送数据的Observable
的数据,而其余 Observable
则被丢弃。
public static void amb() { Observable.ambArray(Observable.just(1, 2, 3).delay(1, TimeUnit.SECONDS) , Observable.just(7, 8, 9).delay(999, TimeUnit.MILLISECONDS)) .subscribe(new Observer<Integer>() { @Override public void onSubscribe(Disposable d) { Log.d(TAG, "onSubscribe"); } @Override public void onNext(Integer value) { Log.d(TAG, "onNext: value = " + value); } @Override public void onError(Throwable e) { Log.d(TAG, "onError: " + e.toString()); } @Override public void onComplete() { Log.d(TAG, "onComplete"); } }); }
输出:
08-12 11:38:43.328 14136 14136 D Operation: onSubscribe
08-12 11:38:44.330 14136 14161 D Operation: onNext: value = 7
08-12 11:38:44.330 14136 14161 D Operation: onNext: value = 8
08-12 11:38:44.330 14136 14161 D Operation: onNext: value = 9
08-12 11:38:44.331 14136 14161 D Operation: onComplete
3.10 defaultIfEmpty()----- 见rxdocs.pdf第196页
作用:在不发送任何有效事件( Nex
t事件)、仅发送了 Complete
事件的前提下,发送一个默认值
public static void defaultIfEmpty() { Observable.empty() .defaultIfEmpty("Andy") .subscribe(new Observer<Object>() { @Override public void onSubscribe(Disposable d) { Log.d(TAG, "onSubscribe"); } @Override public void onNext(Object value) { Log.d(TAG, "onNext: value = " + value); } @Override public void onError(Throwable e) { Log.d(TAG, "onError: " + e.toString()); } @Override public void onComplete() { Log.d(TAG, "onComplete"); } }); }
输出:
08-12 11:48:24.847 14642 14642 D Operation: onSubscribe 08-12 11:48:24.847 14642 14642 D Operation: onNext: value = Andy 08-12 11:48:24.847 14642 14642 D Operation: onComplete
4、总结