https://kotlinlang.org logo
#android
Title
# android
f

Freedom

11/28/2019, 9:23 PM
Can any one help how can I send a request in parallel using coroutines for example I am sending a list of images to the server in parallel or sequentially
p

Paul Woitaschek

11/28/2019, 9:24 PM
images.map { async { send(it) } }.awaitAll()
That's parallel; sequential would be just a
images.forEach { send(it) }
j

joeykaan

11/29/2019, 7:56 AM
There is one good thing to take into account though. If sending those does a data call you might have to change the actual thread those
async
blocks are being executed on. If you read this blog you will see why: https://kotlinexpertise.com/kotlin-coroutines-concurrency/. The
async { send(it) }
will be concurrently but not necessary in parallel.
p

Paul Woitaschek

11/29/2019, 8:30 AM
@joeykaan Is there a difference between wrapping the whole thing within a
withContext(Dispatchers.Default)
vs
async(Dispatchers.Default)
?
j

joeykaan

11/29/2019, 8:33 AM
As far as I can tell, no there is not. Wrapping it in withContext will run it on the Dispatchers.Default and async would do the same 🙂
f

Freedom

11/29/2019, 10:13 PM
Thanks for the answer
3 Views