Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit 2 best practice for android: asynchronous request or synchronous request in AsyncTask?

I am using the Retrofit 2 library for an android REST client. Retrofit itself supports synchronous and asynchronous request (cf. here), the reason for the latter being not to block a thread and thus not to get interrupted by android.

In practice, is it better to use synchronous calls in a native AsyncTask or asynchronous calls directly from Retrofit? If one is preferable over the other, what are the technical reasons?

like image 985
madison54 Avatar asked Nov 14 '15 20:11

madison54


People also ask

Is retrofit asynchronous or synchronous?

Retrofit supports synchronous and asynchronous request execution.

Is retrofit synchronous?

Learn to execute synchronous and asynchronous calls (i.e. blocking and non-blocking calls) in an android app using Retrofit2 and OkHttp library.

How do I make my Android sync calls?

You cannot do synchronous calls on the main thread. Blocking the UI thread on a network call for more than a specified period of time would trigger an ANR. A couple of options would be to use an AysncTask or AsyncTaskLoader.

In which thread Asynctask function will execute?

An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params , Progress and Result , and 4 steps, called onPreExecute , doInBackground , onProgressUpdate and onPostExecute .

What is the difference between synchronous and asynchronous requests in retrofit?

The actual synchronous or asynchronous request is executed differently using the desired method on a later created call object. However, the interface definition for synchronous and asynchronous requests are the same within Retrofit 2. Synchronous methods are executed on the main thread.

What is retrofit for Android?

Get Our Retrofit Book! All modern Android apps need to do network requests. Retrofit offers you an extremely convenient way of creating and managing network requests. From asynchronous execution on a background thread, to automatic conversion of server responses to Java objects, Retrofit does almost everything for you.

What is asynchronous call in Android with example?

Asynchronous Call Example ( Recommended) Asynchronous calls should be made for non-blocking UI. In this way, android executes the request in a separate thread and does not block the main thread. It means the user can still interact with the app while waiting for the response.

How do I implement asynchronous gettasks ()?

Using asynchronous requests forces you to implement a Callback with its two callback methods: success and failure. When calling the asynchronous getTasks () method from a service class, you have to implement a new Callback and define what should be done once the request finishes. The following code snippet illustrates an exemplary implementation.


1 Answers

One of the main reasons to use any of the popular REST clients (retrofit, volley, etc) is that they reduce the amount of details you have manage at the application layer. One of those details is making sure your network requests happen off the main thread. Why would one use an AsyncTask when a library they are already using for other features provides the same functionality with less ceremony? The only reason I can think of is -- you don't think the library's threading is very good. That concern does not apply to retrofit 2, it uses OkHttp to dispatch async calls. OkHttp has been around awhile and used extensively, it manages its own thread pool to execute async requests, and is solid.

So, the upside to using retrofit async is cleaner code, and no downside I know of vs AsyncTask with retrofit sync calls. The only time I use the sync calls is when my code is already executing in a background thread for another reason. I never create a separate thread or asynctask just for the network call and use enqueue instead.

like image 143
iagreen Avatar answered Oct 22 '22 12:10

iagreen