参考< https://www.jianshu.com/p/0cd258eecf60>
1、RxJava调用顺序
<1>Obserable创建
public Observable getRxJavaCreateExampleData() {
return Observable.create(new ObservableOnSubscribe() {
@Override
public void subscribe(ObservableEmitter emitter) {
LogUtils.debug(TAG, "getRxJavaCreateExampleData---:" + 1);
emitter.onNext(1);
LogUtils.debug(TAG, "getRxJavaCreateExampleData---:" + 2);
emitter.onNext(2);
LogUtils.debug(TAG, "getRxJavaCreateExampleData---:" + 3);
emitter.onNext(3);
emitter.onComplete();
LogUtils.debug(TAG, "getRxJavaCreateExampleData---:" + 4);
emitter.onNext(4);
}
});
}
<2>Observer创建&订阅
model.getRxJavaCreateExampleData()
.subscribe(new Observer() {
private Disposable disposable;
@Override
public void onSubscribe(Disposable d) {
disposable = d;
LogUtils.error(TAG, "rxJavaCreateExample---:onSubscribe");
}
@Override
public void onNext(Integer integer) {
LogUtils.error(TAG, "rxJavaCreateExample---:" + integer);
}
@Override
public void onError(Throwable e) {
LogUtils.error(TAG, "rxJavaCreateExample---:onError");
}
@Override
public void onComplete() {
LogUtils.error(TAG, "rxJavaCreateExample---:onComplete");
}
});
调用日志
08-08 10:23:43.126 19790-19790/com.example.zhang E/MainPresenter: rxJavaCreateExample---:onSubscribe
08-08 10:23:43.126 19790-19790/com.example.zhang D/MainModel: getRxJavaCreateExampleData---:1
08-08 10:23:43.128 19790-19790/com.example.zhang E/MainPresenter: rxJavaCreateExample---:1
08-08 10:23:43.128 19790-19790/com.example.zhang D/MainModel: getRxJavaCreateExampleData---:2
08-08 10:23:43.129 19790-19790/com.example.zhang E/MainPresenter: rxJavaCreateExample---:2
08-08 10:23:43.129 19790-19790/com.example.zhang D/MainModel: getRxJavaCreateExampleData---:3
08-08 10:23:43.129 19790-19790/com.example.zhang E/MainPresenter: rxJavaCreateExample---:3
08-08 10:23:43.131 19790-19790/com.example.zhang E/MainPresenter: rxJavaCreateExample---:onComplete
08-08 10:23:43.132 19790-19790/com.example.zhang D/MainModel: getRxJavaCreateExampleData---:4
<3>添加线程调度
model.getRxJavaCreateExampleData()
.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer() {
private Disposable disposable;
@Override
public void onSubscribe(Disposable d) {
disposable = d;
LogUtils.error(TAG, "rxJavaCreateExample---:onSubscribe");
}
@Override
public void onNext(Integer integer) {
LogUtils.error(TAG, "rxJavaCreateExample---:" + integer);
// if (integer == 2) {
// disposable.dispose();
// }
}
@Override
public void onError(Throwable e) {
LogUtils.error(TAG, "rxJavaCreateExample---:onError");
}
@Override
public void onComplete() {
LogUtils.error(TAG, "rxJavaCreateExample---:onComplete");
}
});
调用日志
08-08 10:37:06.349 21365-21365/com.example.zhang E/MainPresenter: rxJavaCreateExample---:onSubscribe
08-08 10:37:06.354 21365-21827/com.example.zhang D/MainModel: getRxJavaCreateExampleData---:1
getRxJavaCreateExampleData---:2
getRxJavaCreateExampleData---:3
getRxJavaCreateExampleData---:4
08-08 10:37:06.355 21365-21365/com.example.zhang E/MainPresenter: rxJavaCreateExample---:1
rxJavaCreateExample---:2
rxJavaCreateExample---:3
rxJavaCreateExample---:onComplete
备注:测试Obserable 数据增加到1000 ,不再是Obserable把数据发送完,Obserer才开始。而是没有什么规律,这就是异步线程。
<4>Disposable中断
model.getRxJavaCreateExampleData()
.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer() {
private Disposable disposable;
@Override
public void onSubscribe(Disposable d) {
disposable = d;
LogUtils.error(TAG, "rxJavaCreateExample---:onSubscribe");
}
@Override
public void onNext(Integer integer) {
LogUtils.error(TAG, "rxJavaCreateExample---:" + integer);
if (integer == 2) {
disposable.dispose();
}
}
@Override
public void onError(Throwable e) {
LogUtils.error(TAG, "rxJavaCreateExample---:onError");
}
@Override
public void onComplete() {
LogUtils.error(TAG, "rxJavaCreateExample---:onComplete");
}
});
日志
08-08 10:49:10.831 23273-23273/com.example.zhang E/MainPresenter: rxJavaCreateExample---:onSubscribe
08-08 10:49:10.835 23273-23706/com.example.zhang D/MainModel: getRxJavaCreateExampleData---:1
getRxJavaCreateExampleData---:2
getRxJavaCreateExampleData---:3
getRxJavaCreateExampleData---:4
08-08 10:49:10.837 23273-23273/com.example.zhang E/MainPresenter: rxJavaCreateExample---:1
rxJavaCreateExample---:2
参考
https://www.jianshu.com/p/464fa025229e