igor.wojda
12/11/2019, 4:23 PMclient A
, client B
). Now client A
calls repository.getUserData()
and this method internally loads data from network. While request is going cliebt B
also calls repository.getUserData()
on the same instance of repository. Can we detekt that network call is in progress and somehow wait for it to finish and return data to both clients?
// Client A:
CoroutineScope(<http://coroutineContentProvider.IO|coroutineContentProvider.IO>).launch {
reository.getUserData()
}
// Client B:
CoroutineScope(<http://coroutineContentProvider.IO|coroutineContentProvider.IO>).launch {
reository.getUserData()
}
class Repository() {
private val job: Job? = null
suspended fun getUserData() {
if(job != null) {
// job is already runned by other client
// can I some how "connect" (wait fo the result) to this job
// instead of proceding and making new network request
}
job = CoroutineScope(<http://coroutineContentProvider.IO|coroutineContentProvider.IO>).launch {
// make network call and return client data
// return network data
}
}
}
bezrukov
12/11/2019, 4:28 PMjob.join
waits for completion, but since you need a result, you could switch from launch
to async
igor.wojda
12/11/2019, 5:06 PMbezrukov
12/11/2019, 5:53 PMclass Repository() {
private val scope = CoroutineScope(SupervisorJob() + <http://Dispatchers.IO|Dispatchers.IO>)
private var activeJob: Deferred<UserData>? = null
suspended fun getUserData(): UserData {
val job = activeJob ?: scope.async{
// make network call and return client data
// return network data
}
activeJob = job
return job.await()
}
}
bezrukov
12/11/2019, 5:57 PMactiveJob
when completed (or just call activeJob?.takeIf { !it.completed }
)