https://blog.csdn.net/nicolelili1/article/details/52165093
一、doOnUnSubscribe
取消订阅时的监听
Observable<Integer> observable = Observable.just(1,2,3,4,5,6);
Subscriber<Integer> subscriber = new Subscriber<Integer>() {
@Override
public void onNext(Integer v) {
Log.e(TAG,"onNext................."+v);
}
@Override
public void onCompleted() {
Log.e(TAG, "onCompleted.................");
}
@Override
public void onError(Throwable e) {
Log.e(TAG, "onError.....................");
}
};
observable
.doOnUnsubscribe(new Action0() {
@Override
public void call() {
Log.e(TAG, "观察者取消订阅了它生成的Observable.....................");
}
})
.subscribe(subscriber);
运行结果:
二、doOnCompleted
Observable正常终止时的监听
Observable<Integer> observable = Observable.just(1,2,3,4,5,6);
Subscriber<Integer> subscriber = new Subscriber<Integer>() {
@Override
public void onNext(Integer v) {
Log.e(TAG,"onNext................."+v);
}
@Override
public void onCompleted() {
Log.e(TAG, "onCompleted.................");
}
@Override
public void onError(Throwable e) {
Log.e(TAG, "onError.....................");
}
};
observable
.doOnCompleted(new Action0() {
@Override
public void call() {
Log.e(TAG, "Observable正常终止了.....................");
}
})
.subscribe(subscriber);
运行结果:
三、doOnError
出错时的监听
Observable<Integer> observable = Observable.create(new Observable.OnSubscribe<Integer>() {
@Override
public void call(Subscriber<? super Integer> subscriber) {
for (int i = 0; i < 5; i++) {
if(i == 3){
subscriber.onError(new Throwable("EROOR"));
}else {
subscriber.onNext(i);
}
try {
Thread.sleep(1000);
} catch (Exception e) {
}
}
subscriber.onCompleted();
}
});
Subscriber<Integer> subscriber = new Subscriber<Integer>() {
@Override
public void onNext(Integer v) {
Log.e(TAG,"onNext................."+v);
}
@Override
public void onCompleted() {
Log.e(TAG, "onCompleted.................");
}
@Override
public void onError(Throwable e) {
Log.e(TAG, "onError.....................");
}
};
observable
.doOnError(new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
Log.e(TAG, "出错了....................."+throwable.toString());
}
})
.subscribe(subscriber);
运行结果:
四、doOnTerminate
订阅即将被终止时的监听,无论是正常终止还是异常终止
Observable<Integer> observable = Observable.create(new Observable.OnSubscribe<Integer>() {
@Override
public void call(Subscriber<? super Integer> subscriber) {
for (int i = 0; i < 5; i++) {
if(i == 3){
subscriber.onError(new Throwable("EROOR"));
}else {
subscriber.onNext(i);
}
try {
Thread.sleep(1000);
} catch (Exception e) {
}
}
subscriber.onCompleted();
}
});
Subscriber<Integer> subscriber = new Subscriber<Integer>() {
@Override
public void onNext(Integer v) {
Log.e(TAG,"onNext................."+v);
}
@Override
public void onCompleted() {
Log.e(TAG, "onCompleted.................");
}
@Override
public void onError(Throwable e) {
Log.e(TAG, "onError.....................");
}
};
observable
.doOnTerminate(new Action0() {
@Override
public void call() {
Log.e(TAG, "订阅即将被终止.....................");
}
})
.subscribe(subscriber);
运行结果:
五、finallyDo
订阅完成之后的监听,无论是正常终止还是异常终止
Observable<Integer> observable = Observable.create(new Observable.OnSubscribe<Integer>() {
@Override
public void call(Subscriber<? super Integer> subscriber) {
for (int i = 0; i < 5; i++) {
if(i == 3){
subscriber.onError(new Throwable("EROOR"));
}else {
subscriber.onNext(i);
}
try {
Thread.sleep(1000);
} catch (Exception e) {
}
}
subscriber.onCompleted();
}
});
Subscriber<Integer> subscriber = new Subscriber<Integer>() {
@Override
public void onNext(Integer v) {
Log.e(TAG,"onNext................."+v);
}
@Override
public void onCompleted() {
Log.e(TAG, "onCompleted.................");
}
@Override
public void onError(Throwable e) {
Log.e(TAG, "onError.....................");
}
};
observable
.finallyDo(new Action0() {
@Override
public void call() {
Log.e(TAG, "订阅已经终止.....................");
}
})
.subscribe(subscriber);
运行结果:
六、delay
延迟一段指定的时间再发射来自Observable的发射物。Delay操作符让原始Observable在发射每项数据之前都暂停一段指定的时间段。效果是Observable发射的数据项在时间上向前整体平移了一个增量。
5s之后数据才打印出来
Integer[]items = {1,2,3,4,5};
Observable<Integer>observable = Observable.from(items).delay(5,TimeUnit.SECONDS).observeOn(Schedulers.newThread()).subscribeOn(Schedulers.newThread());
Subscriber<Integer> subscriber = new Subscriber<Integer>() {
@Override
public void onNext(Integer v) {
Log.e(TAG,"onNext................."+v);
}
@Override
public void onCompleted() {
Log.e(TAG, "onCompleted.................");
}
@Override
public void onError(Throwable e) {
Log.e(TAG, "onError.....................");
}
};
observable.subscribe(subscriber);
运行结果:
七、delaySubscription
延迟订阅源Observable
Integer[]items = {1,2,3,4,5};
Observable<Integer>observable = Observable.from(items);
Subscriber<Integer> subscriber = new Subscriber<Integer>() {
@Override
public void onNext(Integer v) {
Log.e(TAG,"onNext................."+v);
}
@Override
public void onCompleted() {
Log.e(TAG, "onCompleted.................");
}
@Override
public void onError(Throwable e) {
Log.e(TAG, "onError.....................");
}
};
observable
.delaySubscription(2,TimeUnit.SECONDS)
.doOnSubscribe(new Action0() {
@Override
public void call() {
Log.e(TAG, "观察者订阅了它生成的Observable.....................");
}
}).subscribe(subscriber);
订阅后,延迟了2s后onNext函数才被调用
运行结果:
八、timeInterval
返回联系发射的observable的时间间隔
Observable<TimeInterval<Long>>observable = Observable.interval(1,TimeUnit.SECONDS).take(5).timeInterval();
Subscriber<TimeInterval<Long>> subscriber = new Subscriber<TimeInterval<Long>>() {
@Override
public void onNext(TimeInterval<Long> v) {
Log.e(TAG,"onNext................."+v.getValue()+"....................停了"+v.getIntervalInMilliseconds()+"毫s发射了一条数据");
}
@Override
public void onCompleted() {
Log.e(TAG, "onCompleted.................");
}
@Override
public void onError(Throwable e) {
Log.e(TAG, "onError.....................");
}
};
observable.subscribe(subscriber);
运行结果: