1. RxJava2 : 什么是观察者模式
2. RxJava2 : 创建操作符(无关时间)
3. Rxjava2 : 创建操作符(有关时间)
4. Rxjava2 : 变换操作符
5. Rxjava2 : 判断操作符
6. Rxjava2 : 筛选操作符
7. Rxjava2 : 合并操作符
8. Rxjava2 : do操作符
9. Rxjava2 : error处理
10. Rxjava2 : 重试
11. Rxjava2 : 线程切换
api | use |
---|---|
timer | {{Observable.timer}} |
interval | {{Observable.interval}} |
intervalRange | {{Observable.intervalRange}} |
timer
- 指定时间后发送
- 默认发送值为Long类型的0;
- 如果不通过第三个参数指定线程的话,则会采取默认订阅线程(rxjava线程池当中的一条新线程)
- 发送完毕后,会自动发送 onComplete
Log.d(TAG, "in");
Observable.timer(1,TimeUnit.SECONDS)
.subscribe(new Observer<Long>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Long aLong) {
Thread thread = Thread.currentThread();
String name = thread.getName();
Log.d(TAG, "thread:"+name);
Log.d(TAG, "aLong:" + aLong);
}
@Override
public void onError(Throwable e) {
Log.d(TAG, "onError");
}
@Override
public void onComplete() {
Log.d(TAG, "onComplete");
}
});
log
//注意一下时间,是1s后发送
02-12 15:04:01.823 29256-29256/... D/SplashActivity: in
02-12 15:04:02.833 29256-29311/... D/SplashActivity: thread:RxComputationThreadPool-1
02-12 15:04:02.833 29256-29311/... D/SplashActivity: aLong:0
02-12 15:04:02.833 29256-29311/... D/SplashActivity: onComplete
interval
- 默认是无限发送的,可以通过take来截取一部分,之后会介绍take
- 与timer相似,如果不额外指定的话,则会采取默认订阅线程(rxjava线程池当中的一条新线程)
- 与timer相似,无法指定起始值,也为默认的Long类型的0
- 支持指定延时,指定后,会在延时后才开始执行
- 因为会无限发送,所以不会自动发送 onComplete
Log.d(TAG, "in");
Observable.interval(1,TimeUnit.SECONDS)
.subscribe(new Observer<Long>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Long aLong) {
Thread thread = Thread.currentThread();
String name = thread.getName();
Log.d(TAG, "thread:"+name);
Log.d(TAG, "aLong:" + aLong);
}
@Override
public void onError(Throwable e) {
Log.d(TAG, "onError");
}
@Override
public void onComplete() {
Log.d(TAG, "onComplete");
}
});
log
02-12 15:12:25.393 29776-29776/... D/SplashActivity: in
02-12 15:12:26.403 29776-29826/... D/SplashActivity: thread:RxComputationThreadPool-1
02-12 15:12:26.403 29776-29826/... D/SplashActivity: aLong:0
02-12 15:12:27.393 29776-29826/... D/SplashActivity: thread:RxComputationThreadPool-1
02-12 15:12:27.393 29776-29826/... D/SplashActivity: aLong:1
02-12 15:12:28.393 29776-29826/... D/SplashActivity: thread:RxComputationThreadPool-1
02-12 15:12:28.393 29776-29826/... D/SplashActivity: aLong:2
02-12 15:12:29.393 29776-29826/... D/SplashActivity: thread:RxComputationThreadPool-1
02-12 15:12:29.393 29776-29826/... D/SplashActivity: aLong:3
intervalRange
- 和nterval类似,但多了支持了两个参数,也就是range的两个参数,起始值和数量
- 值得注意的是: interval的第一次发送,是间隔之后的时间,而intervalRange因为指定了延迟时间,直接采用延迟时间,并不是延迟时间与间隔时间相加,即使延迟时间可能小于间隔时间
- 指定数量后,则不再是无限发送了
- 发送完毕后,会自动发送 onComplete
//参数1:起始值,参数2:个数,参数3:延迟,参数4:间隔
Log.d(TAG, "in");
Observable.intervalRange(3,5,1,2,TimeUnit.SECONDS)
.subscribe(new Observer<Long>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Long aLong) {
Thread thread = Thread.currentThread();
String name = thread.getName();
Log.d(TAG, "thread:"+name);
Log.d(TAG, "aLong:" + aLong);
}
@Override
public void onError(Throwable e) {
Log.d(TAG, "onError");
}
@Override
public void onComplete() {
Log.d(TAG, "onComplete");
}
});
log
02-12 15:25:00.913 30707-30707/... D/SplashActivity: in
02-12 15:25:01.913 30707-30755/... D/SplashActivity: thread:RxComputationThreadPool-1
02-12 15:25:01.913 30707-30755/... D/SplashActivity: aLong:3
02-12 15:25:03.913 30707-30755/... D/SplashActivity: thread:RxComputationThreadPool-1
02-12 15:25:03.913 30707-30755/... D/SplashActivity: aLong:4
02-12 15:25:05.913 30707-30755/... D/SplashActivity: thread:RxComputationThreadPool-1
02-12 15:25:05.913 30707-30755/... D/SplashActivity: aLong:5
02-12 15:25:07.913 30707-30755/... D/SplashActivity: thread:RxComputationThreadPool-1
02-12 15:25:07.913 30707-30755/... D/SplashActivity: aLong:6
02-12 15:25:09.913 30707-30755/... D/SplashActivity: thread:RxComputationThreadPool-1
02-12 15:25:09.913 30707-30755/... D/SplashActivity: aLong:7
02-12 15:25:09.913 30707-30755/... D/SplashActivity: onComplete