brabo-hi
09/02/2023, 6:46 PMcontext
to the ViewModel. From the documentation, viewModel should be context free. Also what happens when the compose is removed from the backstack but the task is still ongoingStylianos Gakis
09/02/2023, 6:58 PMAlso what happens when the compose is removed from the backstack but the task is still ongoingDo you need the task to stop working as soon as the UI is no longer there, in your case when you pop the backstack?
brabo-hi
09/02/2023, 7:22 PMApplicationContext
for thatFrancesc
09/02/2023, 7:35 PMcontext
in your abstraction. Ideally create an interface and an implementation, with the viewmodel depending on the interface only.brabo-hi
09/02/2023, 7:39 PMclass MyViewModel(app: Application): ViewModel {
fun startTask() {
WorkManager.getInstance(app).enqueue(workRequest)
}
}
My question is to know if this is the best way of doing it ? My issue is regarding passing ApplicationContext
to the ViewModel
Francesc
09/02/2023, 8:20 PMcontext
in viewmodels, as it makes it more challenging to test them. That's why I suggested an abstraction that takes care of enqueuing the job, which you can also easily test by creating a fake that you provide to the viewmodel in your unit testsJoel Denke
09/03/2023, 7:40 AM