Roubert Edgar
05/01/2023, 3:48 PMviewModelScope
.
In my scenario, I want to know when the job was done (success or failure) if the user didn’t leave the screen, and I want to let the view and view model free to be collected by the GC if the user leaves the screen.
I’m reading this Medium Article from Manuel Vivo and I found following solution:
class Repository(
private val externalScope: CoroutineScope,
private val ioDispatcher: CoroutineDispatcher
) {
suspend fun doWork(): Any { // Use a specific type in Result
withContext(ioDispatcher) {
doSomeOtherWork()
return externalScope.async {
// Exceptions are exposed when calling await, they will be
// propagated in the coroutine that called doWork. Watch
// out! They will be ignored if the calling context cancels.
veryImportantOperation()
}.await()
}
}
}
Is this solution the current one or do we have something new for this kind of scenario?Mark Kotyk
05/02/2023, 5:05 PMCoroutineScope
to the caller (like in the ViewModel).