skipe(n)操作符跳过源Obsrvable的前面n个数据项
Observable.range(1, 10).skip(5).subscribe(new Action1<Integer>() {
@Override
public void call(Integer integer) {
LogUtils.d("----->call():" + integer);
}
});
结果:
03-03 16:19:25.340 27107-27107/com.rxandroid.test1 D/----->: ----->call():6
03-03 16:19:25.340 27107-27107/com.rxandroid.test1 D/----->: ----->call():7
03-03 16:19:25.340 27107-27107/com.rxandroid.test1 D/----->: ----->call():8
03-03 16:19:25.340 27107-27107/com.rxandroid.test1 D/----->: ----->call():9
03-03 16:19:25.340 27107-27107/com.rxandroid.test1 D/----->: ----->call():10
skip(long time,TimeUnit) 是跳过多少秒 然后才开始将后面产生的数据提交给订阅者
Observable.interval(1, TimeUnit.SECONDS).sample(3, TimeUnit.SECONDS, Schedulers.newThread()).subscribe(new Observer<Long>() {结果:忽略了0,1
@Override
public void onCompleted() {
LogUtils.d("-------->onCompleted()");
}
@Override
public void onError(Throwable e) {
LogUtils.d("-------->onError()" + e);
}
@Override
public void onNext(Long aLong) {
LogUtils.d("-------->onNext()" + aLong + " thread:" + Thread.currentThread().getName());
}
});
03-03 16:27:42.250 10652-11254/com.rxandroid.test1 D/----->: ----->call():1 time:16:03:42
03-03 16:27:43.250 10652-11254/com.rxandroid.test1 D/----->: ----->call():2 time:16:03:43
03-03 16:27:44.250 10652-11254/com.rxandroid.test1 D/----->: ----->call():3 time:16:03:44
03-03 16:27:45.250 10652-11254/com.rxandroid.test1 D/----->: ----->call():4 time:16:03:45
03-03 16:27:46.250 10652-11254/com.rxandroid.test1 D/----->: ----->call():5 time:16:03:46
03-03 16:27:47.250 10652-11254/com.rxandroid.test1 D/----->: ----->call():6 time:16:03:47
03-03 16:27:48.250 10652-11254/com.rxandroid.test1 D/----->: ----->call():7 time:16:03:48
03-03 16:27:49.250 10652-11254/com.rxandroid.test1 D/----->: ----->call():8 time:16:03:49
03-03 16:27:50.250 10652-11254/com.rxandroid.test1 D/----->: ----->call():9 time:16:03:50
skipLast 正好和skip 相反,忽略最后产生的n个数据项
Observable.range(1, 10).skipLast(5).subscribe(new Action1<Integer>() {
@Override
public void call(Integer integer) {
LogUtils.d("----->call():" + integer + " time:" + DateFormat.format("HH:MM:ss", System.currentTimeMillis()));
}
});
结果:
03-03 16:32:33.240 19805-19805/com.rxandroid.test1 D/----->: ----->call():1 time:16:03:33
03-03 16:32:33.250 19805-19805/com.rxandroid.test1 D/----->: ----->call():2 time:16:03:33
03-03 16:32:33.250 19805-19805/com.rxandroid.test1 D/----->: ----->call():3 time:16:03:33
03-03 16:32:33.250 19805-19805/com.rxandroid.test1 D/----->: ----->call():4 time:16:03:33
03-03 16:32:33.250 19805-19805/com.rxandroid.test1 D/----->: ----->call():5 time:16:03:33
skipLast(long time,TimeUnit) 忽略最后时间单位内产生的数据
例子:
Observable.interval(1, TimeUnit.SECONDS).take(10).skipLast(5, TimeUnit.SECONDS).subscribe(new Action1<Long>() {
@Override
public void call(Long integer) {
LogUtils.d("----->call():" + integer + " time:" + DateFormat.format("HH:MM:ss", System.currentTimeMillis()));
}
});
03-03 16:34:50.860 23169-24170/com.rxandroid.test1 D/----->: ----->call():0 time:16:03:50
03-03 16:34:50.860 23169-24170/com.rxandroid.test1 D/----->: ----->call():1 time:16:03:50
03-03 16:34:52.860 23169-24170/com.rxandroid.test1 D/----->: ----->call():2 time:16:03:52
03-03 16:34:53.860 23169-24170/com.rxandroid.test1 D/----->: ----->call():3 time:16:03:53
03-03 16:34:53.860 23169-24170/com.rxandroid.test1 D/----->: ----->call():4 time:16:03:53
关于take的操作符,想必通过前面的文章,您应该很熟悉,这里还是回顾一下吧
take(n) 并不是娶第n个,而是取前面n个数据
例子:
Observable.range(1,10).take(5).subscribe(new Action1<Integer>() {结果:
@Override
public void call(Integer integer) {
LogUtils.d("-------->onNext()" + integer);
}
});
03-03 16:41:10.790 3005-3005/com.rxandroid.test1 D/----->: -------->onNext()1
03-03 16:41:10.790 3005-3005/com.rxandroid.test1 D/----->: -------->onNext()2
03-03 16:41:10.790 3005-3005/com.rxandroid.test1 D/----->: -------->onNext()3
03-03 16:41:10.790 3005-3005/com.rxandroid.test1 D/----->: -------->onNext()4
03-03 16:41:10.800 3005-3005/com.rxandroid.test1 D/----->: -------->onNext()5
关于 take(longtime, TimeUnit unit) 是在制定时间内取数据,如果超过了这个时间源Observable产生的数据将作废,例子就不演示了
takeFirst操作符和first操作符类似,取满足条件的第一个
区别:
1:first 娶不到要抛异常,takeFirst不会
Observable.range(1, 10).takeFirst(new Func1<Integer, Boolean>() {
@Override
public Boolean call(Integer integer) {
return integer > 10;
}
}).subscribe(new Observer<Integer>() {
@Override
public void onCompleted() {
LogUtils.d("------>takeFirst onCompleted");
}
@Override
public void onError(Throwable e) {
LogUtils.d("------>takeFirst onError:" + e);
}
@Override
public void onNext(Integer integer) {
LogUtils.d("------>takeFirst onNext:" + integer);
}
});
Observable.range(10, 10).first(new Func1<Integer, Boolean>() {
@Override
public Boolean call(Integer integer) {
return integer > 20;
}
}).subscribe(new Observer<Integer>() {
@Override
public void onCompleted() {
LogUtils.d("------>first onCompleted");
}
@Override
public void onError(Throwable e) {
LogUtils.d("------>first onError:" + e);
}
@Override
public void onNext(Integer integer) {
LogUtils.d("------>first onNext:" + integer);
}
});
结果:
03-03 16:50:52.410 21900-21900/com.rxandroid.test1 D/----->: ------>takeFirst onCompleted
03-03 16:50:52.410 21900-21900/com.rxandroid.test1 D/----->: ------>first onError:java.util.NoSuchElementException: Sequence contains no elements
takeLast操作符与last操作符相似,区别在于,如果娶不到满足条件的值,last将抛出异常,这里就不演示了