![]() I really wanted to introduce RX into the calls but I don't have much experience with that. asynchronous vs.Context: My current application uses normal retrofit calls in order to get data from the api. Retrofit API declaration, section synchronous vs.Happy coding! If you run into problems or need help, please find us on Twitter Resources The following code snippet illustrates an exemplary implementation.Ĭall.enqueue(new Callback>() void onResponse(Call> call, Response> response) 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. Using asynchronous requests forces you to implement a Callback with its two callback methods: success and failure. The desired return type is encapsulated into a Call object and the actual request execution defines its type (synchronous/asynchronous). ![]() Our example returns a list of tasks and the Callback does the mapping internally.Īs already mentioned above: the interface definition in Retrofit 2 is the same for synchronous and asynchronous requests. The Callback class is generic and maps your defined return type. Retrofit performs and handles the method execution in a separated thread. Retrofit 1.9 public interface TaskService getTasks(Callback> cb) Instead, the defined method requires a typed callback as last method parameter. Asynchronous requests in Retrofit 1.9 don’t have a return type. Asynchronous RequestsĪdditionally to synchronous calls, Retrofit supports asynchronous requests out of the box. The deserialized response body is available via the. execute() method on a call object will perform the synchronous request in Retrofit 2. Retrofit 1.9 TaskService taskService = ServiceGenerator.createService(TaskService.class) Retrofit 2 TaskService taskService = ServiceGenerator.createService(TaskService.class) The following code snippets illustrate the synchronous request execution with Retrofit and assume that you’re familiar with the ServiceGenerator class introduced in the tutorial on how to create a sustainable Android client. This behavior changed from Retrofit v1 to v2. Let’s move to the point where we execute the actual request. That means, you can still interact with the app itself while waiting for the response. ![]() ![]() Synchronous methods provide the ability to use the return value directly, because the operation blocks everything else during your network request.įor non-blocking UI, you have to handle the request execution in a separated thread by yourself. You’ll run into the `NetworkOnMainThreadException` error. Warning: synchronous requests trigger app crashes on Android 4.0 or newer. That means the UI blocks during request execution and no interaction is possible for this period. Synchronous methods are executed on the main thread. However, the interface definition for synchronous and asynchronous requests are the same within Retrofit 2. The actual synchronous or asynchronous request is executed differently using the desired method on a later created call object. Within Retrofit 2, every request is wrapped into a Call object. ![]() Retrofit 1.9 public interface TaskService getTasks() Retrofit 2 public interface TaskService getTasks() The example below expects a list of tasks as the response when executing the method getTasks. Synchronous requests with Retrofit 1.9 are declared by defining a return type. Users define the concrete execution by setting a return type (synchronous) or not (asynchronous) to service methods. Retrofit supports synchronous and asynchronous request execution. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |