Created
December 22, 2021 09:25
-
-
Save Kashif-E/ff1dcec09f785318fe74eb6856cf3a2e to your computer and use it in GitHub Desktop.
this is how you make parallel api calls using Kotlin coroutines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override suspend fun doNetworkCallsInParallel() { | |
try { | |
CoroutineScope(Dispatchers.IO).launch { | |
val usNewsResponse = async { | |
apiServices.getHeadLines(country = "us") | |
} | |
val healthNewsResponse = async { | |
apiServices.getHeadLines(category = "health") } | |
val x = usNewsResponse.await() | |
val v = healthNewsResponse.await() | |
if (x.isSuccessful && v.isSuccessful){ | |
Log.e("US Response", x.body().toString()) | |
Log.e("Health", v.body().toString()) | |
} | |
} | |
} catch (e: Exception) { | |
Log.e("Exception", e.localizedMessage) | |
} | |
} | |
Happy coding <3 |
@seynth For that there are two ways, you either create two separate retrofit instances and use them to call APIs in parallel, or create separate instances of interceptors to handle two different base URLs, and passing them to retrofit at run time and making async calls
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Kashif-E hey can i call 2 api like 2 baseUrl with retrofit 2 and use the coroutines to get the response?