Roman Abaev
09/23/2024, 10:15 AMclass Repo {
var currentJob: Deferred<Response> = null
suspend fun loadData(): Response = coroutineScope {
if (currentJob.isActive) {
return@coroutineScope currentJob.await()
}
currentJob = async { makeRequest() }
return@coroutineScope currentJob.await()
}
}
So we want to call loadData
multiple times from the different places but make only one request (makeRequest()
) if they were called at the same time.
Is it okay to call await several times?
Is there a better solution?streetsofboston
09/23/2024, 11:53 AMasync
streetsofboston
09/23/2024, 11:58 AMstreetsofboston
09/23/2024, 12:00 PMRoman Abaev
09/23/2024, 12:59 PMstreetsofboston
09/23/2024, 1:13 PMmakeRequest()
call that would set the response back to null
. Be sure to also lock that Mutex while clearing that cached response.baxter
09/24/2024, 1:05 AMloadData()
call won't complete, because you will suspend on coroutineScope
until all child coroutines complete, and that includes your async {}
call there.