Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit default thread

I use Retrofit with RxJava in my Android app, and my code:

public void getConfig(NetworkSubscriber subscriber) {
    Observable<Config> observable = mApi.getConfig();
    observable.subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(subscriber);
}

public void getCode(String mobile, int type, NetworkSubscriber subscriber) {
    Observable<BaseMessageEntity> observable = mApi.getCode(mobile, type);
    observable.subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(subscriber);
}

And i don't want to write .subscribeOn(Schedulers.newThread()) and .observeOn(AndroidSchedulers.mainThread()) every business method

How can i do?

like image 714
xuyanjun Avatar asked Oct 31 '15 17:10

xuyanjun


People also ask

What thread does retrofit use?

2.1. In retrofit, synchronous methods are executed in the main thread. This means that the UI is blocked during the synchronous request execution and no user interaction is possible in this period. In the above example, we have used the ServiceGenerator class.

Does retrofit run on background thread?

Retrofit is a third-party library by Square for communicating with REST APIs over HTTP. Similarly to AsyncTasks , work is done in a background thread, and upon completion it returns the result to the main UI thread. To use Retrofit, declare a service using a simple interface with annotated methods.

Is retrofit thread safe?

Retrofit is a type-safe HTTP client for Android and Java. With Retrofit we can compose the HTTP connection easily through a simple expressive interface just like an api document. Besides the elegant syntax it provides, it's also easy to incorporate with different library.


1 Answers

If you don't want to specify the threads you want on every call, you can create a wrapper around RxJavaCallAdapterFactory to set your threads for your by default.

public class RxThreadCallAdapter extends CallAdapter.Factory {

    RxJavaCallAdapterFactory rxFactory = RxJavaCallAdapterFactory.create();
    private Scheduler subscribeScheduler;
    private Scheduler observerScheduler;

    public RxThreadCallAdapter(Scheduler subscribeScheduler, Scheduler observerScheduler) {
        this.subscribeScheduler = subscribeScheduler;
        this.observerScheduler = observerScheduler;
    }

    @Override
    public CallAdapter<?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
        CallAdapter<Observable<?>> callAdapter = (CallAdapter<Observable<?>>) rxFactory.get(returnType, annotations, retrofit);
        return callAdapter != null ? new ThreadCallAdapter(callAdapter) : null;
    }

    final class ThreadCallAdapter implements CallAdapter<Observable<?>> {
        CallAdapter<Observable<?>> delegateAdapter;

        ThreadCallAdapter(CallAdapter<Observable<?>> delegateAdapter) {
            this.delegateAdapter = delegateAdapter;
        }

        @Override public Type responseType() {
            return delegateAdapter.responseType();
        }

        @Override
        public <T> Observable<?> adapt(Call<T> call) {
            return delegateAdapter.adapt(call).subscribeOn(subscribeScheduler)
              .observeOn(observerScheduler);
        }
    }
}

and then use it instead of RxJavaCallAdapterFactory.create() in your builder --

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("https://api.github.com/")
    .addConverterFactory(GsonConverterFactory.create())
    .addCallAdapterFactory(new RxThreadCallAdapter(Schedulers.io(), AndroidSchedulers.mainThread()))
    .build();
like image 134
iagreen Avatar answered Sep 20 '22 18:09

iagreen