Hi, I’m working with the application scope concept...
# android
r
Hi, I’m working with the application scope concept in my repositories to let some jobs running independent from the
viewModelScope
. 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:
Copy code
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?
🧵 1
m
For us, the pattern is to make repositories contain only suspend functions, and to push up the knowledge of the
CoroutineScope
to the caller (like in the ViewModel).