时间:2021-07-01 10:21:17 帮助过:6人阅读
分别进订阅
- private Observable
allObserver() { - Observable
just; - if (tag) {
- just = Observable.just(1, 2, 3, 4, 5);
- } else {
- just = Observable.just(1, 2, 3, 4, 5, 6);
- }
- tag = true;
- return just.all(integer -> integer < 6);
- }
- private Observable
ambObserver() { - Observable
delay3 = Observable.just(1, 2, 3).delay(3000, TimeUnit.MILLISECONDS); - Observable
delay2 = Observable.just(4, 5, 6).delay(2000, TimeUnit.MILLISECONDS); - Observable
delay1 = Observable.just(7, 8, 9).delay(1000, TimeUnit.MILLISECONDS); - return Observable.amb(delay1, delay2, delay3);
- }
运行结果如下所示。第一次返回的6个数据的Observable不满足所有都小于6的条件,所以结果是false,以后的都满足条件,所以结果是true。使用amb操作符的Observable,第一个发射的数据的是7,所以输出了7,8,9,其他的数据都丢弃了。
- mLButton.setText("all");
- mLButton.setOnClickListener(e -> allObserver().subscribe(i -> log("all:" + i)));
- mRButton.setText("amb");
- mRButton.setOnClickListener(e -> ambObserver().subscribe(i -> log("amb:" + i)));
分别进行订阅
- private Observable
containsObserver() { - if (tag) {
- return Observable.just(1, 2, 3).contains(3);
- }
- tag = true;
- return Observable.just(1, 2, 3).contains(4);
- }
- private Observable
defaultObserver() { - return Observable.create(new Observable.OnSubscribe
() { - @Override
- public void call(Subscriber super Integer> subscriber) {
- subscriber.onCompleted();
- }
- }).isEmpty();
- }
运行结果如下
- mLButton.setText("contains");
- mLButton.setOnClickListener(e -> containsObserver().subscribe(i -> log("contains:" + i)));
- mRButton.setText("isEmpty");
- mRButton.setOnClickListener(e -> defaultObserver().subscribe(i -> log("isEmpty:" + i)));
分别进行订阅
- private Observable
emptyObserver() { - return Observable.create(new Observable.OnSubscribe
() { - @Override
- public void call(Subscriber super Integer> subscriber) {
- subscriber.onCompleted();
- }
- }).defaultIfEmpty(10);
- }
- private Observable
notEmptyObserver() { - return Observable.create(new Observable.OnSubscribe
() { - @Override
- public void call(Subscriber super Integer> subscriber) {
- subscriber.onNext(1);
- subscriber.onCompleted();
- }
- }).defaultIfEmpty(10);
- }
运行结果如下
- mLButton.setText("empty");
- mLButton.setOnClickListener(e -> emptyObserver().subscribe(i -> log("empty:" + i)));
- mRButton.setText("notEmpty");
- mRButton.setOnClickListener(e -> notEmptyObserver().subscribe(i -> log("notEmpty:" + i)));
分别进行订阅
- private Observable
equalObserver() { - return Observable.sequenceEqual(Observable.just(1, 2, 3), Observable.just(1, 2, 3));
- }
- private Observable
notEqualObserver() { - return Observable.sequenceEqual(Observable.just(1, 2, 3), Observable.just(1, 2));
- }
运行结果如下
- mLButton.setText("equal");
- mLButton.setOnClickListener(e -> equalObserver().subscribe(i -> log("equal:" + i)));
- mRButton.setText("notequal");
- mRButton.setOnClickListener(e -> notEqualObserver().subscribe(i -> log("notequal:" + i)));
分别进行订阅
- private Observable
skipUntilObserver() { - return Observable.interval(1, TimeUnit.SECONDS).skipUntil(Observable.timer(3, TimeUnit.SECONDS));
- }
- private Observable
skipWhileObserver() { - return Observable.interval(1, TimeUnit.SECONDS).skipWhile(aLong -> aLong < 5);
- }
运行结果如下
- mLButton.setText("skipUntil");
- mLButton.setOnClickListener(e -> skipUntilObserver().subscribe(i -> log("skipUntil:" + i)));
- mRButton.setText("skipWhile");
- mRButton.setOnClickListener(e -> skipWhileObserver().subscribe(i -> log("skipWhile:" + i)));
分别进行订阅
- private Observable
takeUntilObserver() { - return Observable.interval(1, TimeUnit.SECONDS).takeUntil(Observable.timer(3, TimeUnit.SECONDS));
- }
- private Observable
takeWhileObserver() { - return Observable.interval(1, TimeUnit.SECONDS).takeWhile(aLong -> aLong < 5);
- }
运行结果如下
- mLButton.setText("takeUntil");
- mLButton.setOnClickListener(e -> takeUntilObserver().subscribe(i -> log("takeUntil:" + i)));
- mRButton.setText("takeWhile");
- mRButton.setOnClickListener(e -> takeWhileObserver().subscribe(i -> log("takeWhile:" + i)));
http://www.bkjia.com/PHPjc/1077808.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1077808.htmlTechArticleRxJava操作符(七)Conditional and Boolean 在这篇文章里我们来了解一下条件和布尔操作符。 一、All/Amb All操作符根据一个函数对源Observable发射的...