Retrofit cancelling multiple API calls

Tram Ho

Today we will see how to use Retrofit to call an API multiple times and cancel all previous calls when making a new call. In addition, we will see how to use coroutines with Retrofit.

Use-Case: When implementing the search function in the App – we request the API every time the user enters something in EditText (Trying to get the search done in real time) and cancels all API calls before.

First, we need to add the Retrofit and Coroutines to the Gradle file. Add these lines to App-level build.gradle :

Now create an interface for retrofit named ApiService.

We use the demo API from https://demo.dataverse.org , this is a free demo API service we use for testing. It will provide search results according to the user’s query.

We use the Kotlin suspend function instead of the normal function because we will use coroutines to manage the API.

Now create a data class for this API named SearchResultModel .

Now, create an API provider object file named ApiProvider , this object will be used to create a retrofit instance:

Here we are creating the OkHttp client to log requests and response APIs and set up Retrofit. The retrofit variable, used to get an instance of the Retrofit client.

We will use repository classes / objects to process data.

So, we create an object called DataRepository :

Here, we first created an instance of APIService in the init block, to initialize retrofit whenever the DataRepository was initialized.

Note: Since we are using an object instead of a class, only one instance will be created and shared on the application.

Now we will create a generic data class , this will be a wrapper class to receive all responses from API calls. We use a wrapper class with LiveData so we can notify our views whenever there’s data from the API.

Create a data class named GenericDataModel .

Also, note that we are declaring the genetic T data type so we can reuse this class for all Responses types.

Now this is the last part of the API call in practice. In Activity, create a function named startSearching pass the parameter of type String . This method will be called each time when the user types anything to search:

Now we will create a Coroutine in function startSearching , it will call the API and perform the rest of the process in a background thread, and finally it sends the results back to the main thread.

In the above method, we can see that we have provided Dispatchers.IO for coroutine scope, which means that everything inside will be called in the background thread. Also, the withContext(Dispatchers.Main) function inside Coroutine will run anything inside it back to the main thread.

Now, take an instance of coroutine and create a global variable to store that instance. This is done to avoid creating multiple coroutines for each call and canceling the previous request before performing a new search. Create a global variable of type Job and return an instance of coroutine to this variable in the startSearching method.

Now here all the important things happen, we don’t need to manage anything here. We don’t have to wait for a response from the API, no need to use any callbacks. All of this is managed by retrofit itself because they support coroutines with suspend functions.

Now, add TextWatcher on EditText to listen to all the facts whenever users start typing to begin the Search Query API calls. Alternatively, we can create an extension function for this.

Note that inside afterTextChanged , we canceled CoroutineJob earlier so it automatically canceled the previous API calls. Then call startSearching method so the new API call will be made.

This is how we can manage the cancellation of previous API calls when making new calls to only get results for the very latest and last API calls.

Ref: https://proandroiddev.com/retrofit-cancelling-multiple-api-calls-4dc6b7dc0bbd

Share the news now

Source : Viblo