本文主要是介绍rxjava : 常用的 compose 与 ObservableTransformer,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
RxJavaUtils 工具类
public class RxJavaUtils {public static <T> ObservableTransformer<T, T> applySchedulers() {return new ObservableTransformer<T, T>() {@Overridepublic ObservableSource<T> apply(Observable<T> upstream) {return upstream.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());}};}public static ObservableTransformer<Response<ResponseBody>, Boolean> checkSuccessCode() {return new ObservableTransformer<Response<ResponseBody>, Boolean>() {@Overridepublic ObservableSource<Boolean> apply(Observable<Response<ResponseBody>> upstream) {return upstream.map(new Function<Response<ResponseBody>, Boolean>() {@Overridepublic Boolean apply(Response<ResponseBody> response) throws Exception {return response != null && response.code() == 204;}});}};}public static <T> ObservableTransformer<T, T> preventDuplicateClicks(long interval) {return new ObservableTransformer<T, T>() {@Overridepublic ObservableSource<T> apply(Observable<T> upstream) {return upstream.throttleFirst(interval, TimeUnit.MILLISECONDS);}};}//lambda简化版// public static <T> ObservableTransformer<T, T> applySchedulers() {
// return upstream -> upstream.subscribeOn(Schedulers.io())
// .observeOn(AndroidSchedulers.mainThread());
// }
//
// public static ObservableTransformer<Response<ResponseBody>, Boolean> checkSuccessCode() {
// return upstream -> upstream.map((Function<Response<ResponseBody>, Boolean>)
// response -> response != null && response.code() == 204);
// }
//
// public static <T> ObservableTransformer<T, T> preventDuplicateClicks(long interval) {
// return upstream -> upstream.throttleFirst(interval, TimeUnit.MILLISECONDS);
// }}
测试:
Disposable disposable = Observable.just(1, 2, 3).compose(RxJavaUtils.<Integer>applySchedulers()).compose(RxJavaUtils.<Integer>preventDuplicateClicks(1000)).subscribe(integer -> {});
这篇关于rxjava : 常用的 compose 与 ObservableTransformer的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!