Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit callback on main thread

With a call like :

@GET("/user/{id}/data")
void getUserData(@Path("id") int id, Callback<Data> cb);

Callback is supposed to be executed on the main thread (if not using RxJava). My questions are :

  1. Where does the parsing happen (let's assume I am using an XML converter for process response). Is this main thread, or a different one ? Does it depend on converter implementation ?
  2. If I have to include some (heavy) validation rule/business rules, do I need to spawn a new thread inside callable ? Or is it fine to have it done in the Callback methods ?

I am looking for ways to get data in my activity from a web-service avoiding thread-management on my own (or using other approaches like IntentService, etc.), but am afraid of using RxJava either (because of experimental support). Is there another suggested approach to deal with this problem ?

like image 895
dev Avatar asked Feb 08 '14 22:02

dev


People also ask

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.

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.

Is retrofit thread safe?

Retrofit is a type-safe HTTP client for Android and Java.

What is callback in retrofit?

public interface Callback<T> Communicates responses from a server or offline requests. One and only one method will be invoked in response to a given request. Callback methods are executed using the Retrofit callback executor.


1 Answers

Where does the parsing happen (let's assume I am using an XML converter for process response). Is this main thread, or a different one ? Does it depend on converter implementation ?

Always a background thread no matter the converter you use.

If I have to include some (heavy) validation rule/business rules, do I need to spawn a new thread inside callable ? Or is it fine to have it done in the Callback methods ?

This is fairly subjective and there's a lot of ways to tackle it. The Callback will be executed on the main thread by default.

You can change the thread on which callbacks will be invoked by supplying a custom Executor to the RestAdapter.Builder. This will affect all services constructed by that RestAdapter, however, which may not be what you want.

There's nothing wrong with spawning another thread (or enqueueing on an executor) from the Callback if the work you want to be done can be done in parallel with updating the UI (for example, light-weight caching).

If the expensive work must be done before notifying the UI then you are better off switching the method to be synchronous (no callback) and doing the threading yourself. This way you can do expensive operations before and after the HTTP call (file i/o, caching, transforming, validating, etc.).

We currently use RxJava (of which Retrofit has experimental support) for what you are asking:

interface Foo {
  @GET("/")
  Observable<Foo> getFoo(String bar);
}

foo.getFoo()
  .mapMany(new ExpensiveOperationFunction())
  .observeOn(AndroidSchedulers.mainThread())
  .subscribe(new Observer<TransformedFoo>() { .. });
like image 63
Jake Wharton Avatar answered Sep 29 '22 00:09

Jake Wharton