在Rxjava中Observable 有 Hot 和 Cold之分.
Hot Observable:
Hot Observable无论有没有观察者进行订阅事件始终都会发生. 当Hot Observable 有多个订阅者时(即多个订阅者进行订阅时),
Hot Observable与订阅者们的关系是一对多的关系,可以与多个订阅者共享信息.
Cold Observable:
Cold Observable是只有观察者进行订阅了,才开始执行发射数据流的代码.并且Cold Observable和Observer只能是一对一的关系,
当有多个不同的订阅者时,上游的Observable每有一个订阅者订阅一次就会重新执行一次发射数据流的代码. 也就是说,对于Cold Observable而言,有多个Observer的时候它们各自的事件是独立存在的
每个订阅者都会独立的接收到它们的数据流
1.Cold Observable 冷的Observable
Observable<Long> observable = Observable.create(new ObservableOnSubscribe<Long>() {
@Override
public void subscribe(final ObservableEmitter<Long> e) throws Exception {
Log.d(TAG,"subscribe : " + " Thread = " + Thread.currentThread().getName());
Observable.interval(0, 100, TimeUnit.MILLISECONDS, Schedulers.computation())
.take(10)
.subscribe(new Consumer<Long>() {
@Override
public void accept(@NonNull Long aLong) throws Exception {
Log.d(TAG, "上游发射了 : " + aLong.longValue() + " Thread = " + Thread.currentThread().getName());
e.onNext(aLong);
if(aLong.longValue() >= 9){
e.onComplete();
}
}
});
}
}).observeOn(Schedulers.newThread());
//通过Observable的create just fromXxx range timer等这些创建的都是Cold Observable即冷的Observable对象
Observer<Long> observer1 = new Observer<Long>() {
@Override
public void onSubscribe(Disposable d) {
Log.d(TAG, "onSubscribe1 开始订阅 " + Thread.currentThread().getName());
}
@Override
public void onNext(Long aLong) {
Log.d(TAG, "onNext1 : " + aLong.longValue() + " Thread = " + Thread.currentThread().getName());
}
@Override
public void onError(Throwable e) {
Log.d(TAG, "onError1 : " + e.getMessage() + " Thread = " + Thread.currentThread().getName());
}
@Override
public void onComplete() {
Log.d(TAG, "onComplete1 : Thread = " + Thread.currentThread().getName());
}
};
Observer observer2 = new Observer<Long>() {
@Override
public void onSubscribe(Disposable d) {
Log.d(TAG,"onSubscribe2 开始订阅 " + Thread.currentThread().getName());
}
@Override
public void onNext(Long aLong) {
Log.d(TAG,"onNext2 : " + aLong.longValue() + " Thread = " + Thread.currentThread().getName());
}
@Override
public void onError(Throwable e) {
Log.d(TAG,"onError2 : " + e.getMessage() + " Thread = " + Thread.currentThread().getName());
}
@Override
public void onComplete() {
Log.d(TAG,"onComplete2 : Thread = " + Thread.currentThread().getName());
}
};
//下游的两个订阅者分别在不同的时间进行事件的订阅
observable.subscribe(observer1);
SystemClock.sleep(500);
observable.subscribe(observer2);
打印结果:
从打印结果上可以看出这两个订阅者在不同的时间点上订阅了上游的Observable 但是他们接收的数据是一样的
各自相互独立 只是在同一时刻他们接收的数据是不同的
2.通过publish()操作符将我们的Cold Observable转化成Hot Observable
//通过publish()操作符将我们的Cold Observable转化成Hot Observable
ConnectableObservable<Long> publish = Observable.create(new ObservableOnSubscribe<Long>() {
@Override
public void subscribe(final ObservableEmitter<Long> e) throws Exception {
Log.d(TAG,"subscribe : 开始发射数据" + " Thread = " + Thread.currentThread().getName());
Observable.interval(0, 100, TimeUnit.MILLISECONDS, Schedulers.computation())
.take(5)
.subscribe(new Consumer<Long>() {
@Override
public void accept(@NonNull Long aLong) throws Exception {
Log.d(TAG, "上游发射了 : " + aLong.longValue() + " Thread = " + Thread.currentThread().getName());
e.onNext(aLong.longValue());
if (aLong.longValue() >= 4) {
e.onComplete();
}
}
});
}
}).observeOn(Schedulers.newThread())
//通过publish()操作符将我们的Cold Observable转化成为一个ConnectableObservable的Hot Observable
.publish();
//生成的ConnectableObservable需要调用connect()才能够真正执行上游发射数据流的代码
Disposable disposable = publish.connect();
Observer<Long> observer1 = new Observer<Long>() {
@Override
public void onSubscribe(Disposable d) {
Log.d(TAG, "onSubscribe1 开始订阅 " + Thread.currentThread().getName());
}
@Override
public void onNext(Long aLong) {