Can any one help how can I send a request in paral...
# android
f
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
images.map { async { send(it) } }.awaitAll()
That's parallel; sequential would be just a
images.forEach { send(it) }
j
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
@joeykaan Is there a difference between wrapping the whole thing within a
withContext(Dispatchers.Default)
vs
async(Dispatchers.Default)
?
j
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
Thanks for the answer