Parallel Multiple Network Calls Using Kotlin Coroutines
Application’s home screen is where we need to populate multiple views like Promotional banners, List to show core content of the app, Basic user info, etc., It is really easy to fill all these views with data when you get all the required data in a single API call, but that’s, not the case every time. Many times we need to call multiple API requests to get the required data on client devices and wait for all of them to execute one after the other, fetch the result, and process it. We are going to learn how to make Parallel Multiple Network Calls Using Kotlin Coroutines. We have learned to write the code inside the ViewModel with Kotlin Coroutines and LiveData that follows a basic MVVM Architecture. This tutorial will also help you in doing any type of background tasks in parallel using Kotlin Coroutines.
And Yes the Progress view is still Spinning in the home screen, So how about we take down the spinner much earlier than before by executing the API requests in parallel let us look at the code first.
When you have to make different API calls and wait, you do something like this:
but what happens if I’ve to do multiple concurrent same API Calls with different parameters:
The problem with the second approach is that it will go through all ids of multiple lists and make async calls, but the content will be posted probably before that. How can I wait for all the responses for each loop to be finished and only then postValue of the content to view? so yeah, that’s the basic idea about Parallel Multiple Network Calls Using Kotlin Coroutines
Please Login to comment...