一、map语法练习
1、Obserable创建
public Observable<Integer> getRxJavaCreateExampleData() {
return Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> emitter) {
LogUtils.debug(TAG, "getRxJavaCreateExampleData---:"+Thread.currentThread().getName()+"--:" + 1);
emitter.onNext(1);
LogUtils.debug(TAG, "getRxJavaCreateExampleData---:" +Thread.currentThread().getName()+"--:"+ 2);
emitter.onNext(2);
LogUtils.debug(TAG, "getRxJavaCreateExampleData---:"+Thread.currentThread().getName()+"--:" + 3);
emitter.onNext(3);
emitter.onComplete();
LogUtils.debug(TAG, "getRxJavaCreateExampleData---:" +Thread.currentThread().getName()+"--:"+ 4);
emitter.onNext(4);
}
});
}
2、观察者-Consumer创建
public void rxJavaMapExpamle() {
Disposable subscribe = model.getRxJavaCreateExampleData()
.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.map(new Function<Integer, String>() {
@Override
public String apply(Integer integer) throws Exception {
LogUtils.error(TAG, "rxJavaMapExpamle--Function--:"+Thread.currentThread().getName()+"--:" + integer);
return "this is from map " + integer;
}
}).subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
LogUtils.error(TAG, "rxJavaMapExpamle--Consumer--:"+Thread.currentThread().getName()+"--:" + s);
}
});
compositeDisposable.add(subscribe);
}
3、日志
08-08 14:09:57.556 13443-14015/com.example.zhang D/MainModel: getRxJavaCreateExampleData---:RxCachedThreadScheduler-1--:1
08-08 14:09:57.557 13443-14015/com.example.zhang D/MainModel: getRxJavaCreateExampleData---:RxCachedThreadScheduler-1--:2
getRxJavaCreateExampleData---:RxCachedThreadScheduler-1--:3
getRxJavaCreateExampleData---:RxCachedThreadScheduler-1--:4
08-08 14:09:57.557 13443-13443/com.example.zhang E/MainPresenter: rxJavaMapExpamle--Function--:main--:1
rxJavaMapExpamle--Consumer--:main--:this is from map 1
rxJavaMapExpamle--Function--:main--:2
08-08 14:09:57.558 13443-13443/com.example.zhang E/MainPresenter: rxJavaMapExpamle--Consumer--:main--:this is from map 2
rxJavaMapExpamle--Function--:main--:3
rxJavaMapExpamle--Consumer--:main--:this is from map 3
总结
1、map 基本作用就是将一个 Observable 通过某种函数关系,转换为另一种 Observable,上面例子中就是把我们的 Integer 数据变成了 String 类型
二、修改线程调度
public void rxJavaMapExpamle() {
Disposable subscribe = model.getRxJavaCreateExampleData()
.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.map(new Function<Integer, String>() {
@Override
public String apply(Integer integer) throws Exception {
LogUtils.error(TAG, "rxJavaMapExpamle--Function--:" + Thread.currentThread().getName() + "--:" + integer);
return "this is from map " + integer;
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
LogUtils.error(TAG, "rxJavaMapExpamle--Consumer--:" + Thread.currentThread().getName() + "--:" + s);
}
});
compositeDisposable.add(subscribe);
}
日志
08-08 14:12:58.857 14348-14738/com.example.zhang D/MainModel: getRxJavaCreateExampleData---:RxCachedThreadScheduler-1--:1
08-08 14:12:58.857 14348-14738/com.example.zhang E/MainPresenter: rxJavaMapExpamle--Function--:RxCachedThreadScheduler-1--:1
08-08 14:12:58.857 14348-14738/com.example.zhang D/MainModel: getRxJavaCreateExampleData---:RxCachedThreadScheduler-1--:2
08-08 14:12:58.857 14348-14738/com.example.zhang E/MainPresenter: rxJavaMapExpamle--Function--:RxCachedThreadScheduler-1--:2
08-08 14:12:58.857 14348-14738/com.example.zhang D/MainModel: getRxJavaCreateExampleData---:RxCachedThreadScheduler-1--:3
08-08 14:12:58.857 14348-14738/com.example.zhang E/MainPresenter: rxJavaMapExpamle--Function--:RxCachedThreadScheduler-1--:3
08-08 14:12:58.858 14348-14738/com.example.zhang D/MainModel: getRxJavaCreateExampleData---:RxCachedThreadScheduler-1--:4
08-08 14:12:58.860 14348-14348/com.example.zhang E/MainPresenter: rxJavaMapExpamle--Consumer--:main--:this is from map 1
rxJavaMapExpamle--Consumer--:main--:this is from map 2
rxJavaMapExpamle--Consumer--:main--:this is from map 3