david.bilik
12/18/2019, 1:58 PMoriginal function
override suspend fun user(): User {
return api.user()
}
suggested solution
var userDeferred: Deferred<User>? = null
override suspend fun user(): User {
if (userDeferred == null) {
userDeferred = async {
apiInteractor.user()
}
}
return userDeferred.await().also {
userDeferred = null
}
}uhe
12/18/2019, 2:07 PMuserDeferred is a nullable property, so userDeferred.await() is not allowed.
Furthermore, you can still get multiple concurrent calls, because the check for null and the assignment are not atomic (if this function is called from different threads).uhe
12/18/2019, 2:12 PMMutex and wrap the whole body in a mutex.withLock .
However, actually piggy-backing on in-flight calls would require another solution.david.bilik
12/18/2019, 2:32 PMMutex solution, thanks