For more articles please click : http://77blogs.com/?p=170
Reproduced, please indicate the source: https://www.cnblogs.com/tangZH/p/12088332.html , http://77blogs.com/?p=170
Use Scene One:
Now performs two tasks:
1, the output string 0
2, the output string 1
We can use concat to implement multiple data sources.
1, the output string 0 Source:
Observable observableLocal = Observable.create(new ObservableOnSubscribe() {
@Override
public void subscribe(ObservableEmitter emitter) throws Exception {
emitter.onNext("0");
emitter.onComplete();
}
}).subscribeOn(Schedulers.io());
2, the output string 1 Source:
Observable observableNet = Observable.create(new ObservableOnSubscribe() {
@Override
public void subscribe(ObservableEmitter emitter) throws Exception {
emitter.onNext("1");
emitter.onComplete();
}
}).subscribeOn(Schedulers.io());
3, receives a plurality of data sources:
Observable.concat(observableLocal, observableNet)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer() {
@Override
public void accept(Object o) throws Exception {
Log.d(TAG, (String) o);
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
Log.d(TAG, throwable.getMessage());
}
});
}
We can see Log:
12-23 20:23:48.771 23643-23643/com.status.rxjavasample D/RxJavaHelper: 0
12-23 20:23:48.771 23643-23643/com.status.rxjavasample D/RxJavaHelper: 1
Two strings output, and it is ordered.
Use Scene II,
Data acquisition, if acquired data obtained from the local cache, so it will not get from the network, or obtained from the network.
We 1,2 respectively as the above two steps to obtain data from the local cache and retrieve data from the network buffer, then we need to change the step 3 above.
Observable.concat(observableLocal, observableNet)
.firstElement()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer() {
@Override
public void accept(Object o) throws Exception {
Log.d(TAG, (String) o);
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
Log.d(TAG, throwable.getMessage());
}
});
}
The only difference is plus .firstElement ().
log output is as follows:
12-23 20:29:11.731 24458-24458/com.status.rxjavasample D/RxJavaHelper: 0
firstElement operator: traversing the viewer accordance with the order event, the event is not empty, then stop the walk.