Jilles Soeters
08/05/2021, 7:18 PMList<String>
with about 250 URLs. Currently I am doing a GET
request to each url in a forEach loop so it’s synchronous. Obviously one of Kotlin’s amazing features is co-routines. Does anyone know of a good resource on how I could do this async? It’s currently taking 30 seconds while I feel like this could be done much much quicker async. Note I own all 250 URLs that are getting visited so I am not spamming anyoneArslan Armanuly
08/05/2021, 7:24 PMArslan Armanuly
08/05/2021, 7:25 PMArslan Armanuly
08/05/2021, 7:25 PMArslan Armanuly
08/05/2021, 7:28 PMCLOVIS
08/05/2021, 8:43 PMval yourList: List<String> = ...
val results = yourList
.map { scope.async { yourFunction(it) } }
.awaitAll()
The above links should explain where scope
etc comes from.CLOVIS
08/05/2021, 8:44 PMasync
multiple times before calling await
.gildor
08/06/2021, 1:55 AMephemient
08/06/2021, 2:10 AMephemient
08/06/2021, 2:26 AMval yourList: List<String> = ...
val results = yourList.asFlow()
.flatMapMerge(concurrency = 8) { suspend { yourFunction(it) }.asFlow() }
.toList()
although it doesn't preserve ordergildor
08/06/2021, 2:29 AMgildor
08/06/2021, 2:29 AMephemient
08/06/2021, 2:34 AMgildor
08/06/2021, 2:35 AMgildor
08/06/2021, 2:39 AMgildor
08/06/2021, 2:41 AMYoussef Shoaib [MOD]
08/06/2021, 3:05 PMSemaphore
from kotlinx.coroutines