bsimmons
08/28/2020, 11:56 AMDeferred<T>
s and reuse them? Or is this a coroutine anti-pattern? I'm pretty new to coroutines so I'm just wonder if there is a better way.
private val notesDeferred = HashMap<String, Deferred<Resource<String>>>()
suspend fun getNotes(id: String): Resource<String>{
if(notesDeferred.containsKey(id) && notesDeferred.get(id)!!.isActive){
return notesDeferred.get(id)!!.await()
} else {
val newRequest = GlobalScope.async {
remoteSource.getNotes(id)
}
notesDeferred.put(id, newRequest)
return newRequest.await()
}
}
wcaokaze
08/28/2020, 12:46 PMwcaokaze
08/28/2020, 12:53 PMbsimmons
08/28/2020, 1:04 PMwcaokaze
08/28/2020, 1:15 PMasync
starts after the first async
finished. In this case, the second async
do the task as usual (not await
the first task).bsimmons
08/28/2020, 1:26 PM.isActive
to do that.