RxJavaCallAdapterFactory默认实现异步调度

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


经常重复的异步调度代码:

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

         .subscribe();

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


final class RxJava2CallAdapter implements CallAdapter {
 
  @Override public Object adapt(Call call) {
    Observable> 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>) callAdapter, f -> f.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()));
        }
        if (isFlowable) {
            return CallAdapterFactory.create((CallAdapter, Flowable>) callAdapter, f -> f.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()));
        }
        //省略...
        return callAdapter;
    }
}

创建CallAdapterFactory工厂类

public class CallAdapterFactory implements CallAdapter {

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

    private Function mFunction;

    @Override
    public Type responseType() {
        return mAdapter.responseType();
    }
    @Override
    public W adapt(Call 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;
    }
}

在创建Retrofit时添加自定义RxJava2CallAdapterFactory

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

使用时不再需要添加异步调度代码: 

sevice.XXX().subscribe();

 

你可能感兴趣的:(Retrofit,适配器,源码分析)