RxJavaCallAdapterFactory默认实现异步调度

默认添加异步调度


重复的异步调度代码:

设计给Retrofit代理的Flowable 和 Observable的对象默认添加异步调度

  Sevice.XXX()
         .subscribeOn(Schedulers.io())
         .observeOn(AndroidSchedulers.mainThread())

         .subscribe();

根据RxJava2CallAdapterFactoryget方法返回一个CallAdapter的实现类RxJava2CallAdapter,其adapt 方法经过处理得到我们需要的可以操作的对象observable 或者Flowable


 源码分析

final class RxJava2CallAdapter<R> implements CallAdapter<R, Object> {
 
  @Override public Object adapt(Call<R> call) {
    Observable<Response<R>> responseObservable = isAsync
        ? new CallEnqueueObservable<>(call)
        : new CallExecuteObservable<>(call);
    Observable<?> observable;
    if (isResult) {
      observable = new ResultObservable<>(responseObservable);
    } else if (isBody) {
      observable = new BodyObservable<>(responseObservable);
    } else {
      observable = responseObservable;
    }
    if (scheduler != null) {
      observable = observable.subscribeOn(scheduler);
    }
    if (isFlowable) {
      return observable.toFlowable(BackpressureStrategy.LATEST);
    }
     //省略...
    return observable;
  }
}

自定义CallAdapter.Factory实现委托RxJava2CallAdapterFactory对象实现添加异步调度: 



public final class ArnoldRxJava2CallAdapterFactory extends CallAdapter.Factory {

    private CallAdapter.Factory mFactory;

    public static ArnoldRxJava2CallAdapterFactory create() {
        return new ArnoldRxJava2CallAdapterFactory();
    }
    private ArnoldRxJava2CallAdapterFactory() {
        mFactory = RxJava2CallAdapterFactory.create();
    }
    @Override
    public CallAdapter<?, ?> get(@NonNull Type returnType, @NonNull Annotation[] annotations, @NonNull Retrofit retrofit) {
        final CallAdapter<?, ?> callAdapter = mFactory.get(returnType, annotations, retrofit);

        Class<?> rawType = getRawType(returnType);

        boolean isFlowable = rawType == Flowable.class;
        boolean isObservable = rawType == Observable.class;

        if (null == callAdapter) {
            return null;
        }
        //生产不同的CallAdapter
        if (isObservable) {
            return CallAdapterFactory.create((CallAdapter<Observable<?>, Observable<?>>) callAdapter, f -> f.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()));
        }
        if (isFlowable) {
            return CallAdapterFactory.create((CallAdapter<Flowable<?>, Flowable<?>>) callAdapter, f -> f.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()));
        }
        //省略...
        return callAdapter;
    }
}

创建CallAdapterFactory工厂类

public class CallAdapterFactory<R, W> implements CallAdapter<R, W> {

    public static <R, W> CallAdapterFactory<R, W> create(CallAdapter<R, W> mAdapter, Function<W, W> mFunction) {
        return new CallAdapterFactory<R, W>(mAdapter, mFunction);
    }
    private CallAdapterFactory(CallAdapter<R, W> mAdapter, Function<W, W> mFunction) {
        this.mAdapter = mAdapter;
        this.mFunction = mFunction;
    }
    private CallAdapter<R, W> mAdapter;

    private Function<W, W> mFunction;

    @Override
    public Type responseType() {
        return mAdapter.responseType();
    }
    @Override
    public W adapt(Call<R> call) {
        W adapt = mAdapter.adapt(call);
        if (mFunction == null) {
            return adapt;
        }
        try {
            adapt = mFunction.apply(mAdapter.adapt(call));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return adapt;
    }
}

调用示例

        T service= new Retrofit.Builder()
                .client(OkHttpClient client)
                .addConverterFactory(ArnoldGsonConverterFactory.create())
                .addCallAdapterFactory(ArnoldRxJava2CallAdapterFactory .create())
                .baseUrl(BASE_HTTP_URL)
                .build()
                .create(Class<T> service);

在创建Retrofit时添加自定义RxJava2CallAdapterFactory,使用时不再需要添加异步调度代码: sevice.XXX().subscribe();

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值