rxjava切换线程避免重复代码

时间:2022-09-30 17:45:50
.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(subscriber);

每次切换线程的操作

 

变换封装操作

Observable.Transformer schedulersTransformer() {
return new Observable.Transformer() {
@Override
public Object call(Object observable) {
return ((Observable) observable).subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
};
}

使用的时候

RetrofitClient.singletonDemoService("http://api/data/")
.requestNet(
"xx","1")
.compose(schedulersTransformer())
.subscribe(subscriber);

水一波