Florian
10/29/2020, 3:33 PMclass DeleteAllCompletedViewModel @ViewModelInject constructor(
private val taskDao: TaskDao,
@ApplicationScope private val applicationScope: CoroutineScope
) :
ViewModel() {
fun deleteCompletedTasks() = applicationScope.launch {
taskDao.deleteCompletedTasks()
}
}
Ian Lake
10/29/2020, 4:05 PMlaunch(NonCancellable)
Florian
10/29/2020, 4:27 PMIan Lake
10/29/2020, 6:37 PMlouiscad
10/29/2020, 6:37 PMFlorian
10/29/2020, 6:55 PMFlorian
10/29/2020, 6:55 PMlouiscad
10/29/2020, 7:17 PMGlobalScope
or a custom AppScope
might be the most pragmatic solution.Florian
10/29/2020, 7:22 PMlouiscad
10/29/2020, 7:28 PMIan Lake
10/29/2020, 7:37 PMNonCancellable
means you rely on the signal from the database to know when that operation finishes, while injecting some scope lets you wait for its work to complete. If you aren't doing that specific kind of testing though, then the shortest, most concise code gets my vote 🤷louiscad
10/29/2020, 7:38 PMjoin()
on GlobalScope.launch { … }
no problem