@@ -19,13 +19,13 @@ package com.google.samples.apps.sunflower.viewmodels
19
19
import androidx.lifecycle.LiveData
20
20
import androidx.lifecycle.Transformations
21
21
import androidx.lifecycle.ViewModel
22
+ import androidx.lifecycle.viewModelScope
22
23
import com.google.samples.apps.sunflower.PlantDetailFragment
23
24
import com.google.samples.apps.sunflower.data.GardenPlantingRepository
24
25
import com.google.samples.apps.sunflower.data.Plant
25
26
import com.google.samples.apps.sunflower.data.PlantRepository
26
- import kotlinx.coroutines.CoroutineScope
27
- import kotlinx.coroutines.Dispatchers.Main
28
- import kotlinx.coroutines.Job
27
+ import kotlinx.coroutines.ExperimentalCoroutinesApi
28
+ import kotlinx.coroutines.cancel
29
29
import kotlinx.coroutines.launch
30
30
31
31
/* *
@@ -40,27 +40,13 @@ class PlantDetailViewModel(
40
40
val isPlanted: LiveData <Boolean >
41
41
val plant: LiveData <Plant >
42
42
43
- /* *
44
- * This is the job for all coroutines started by this ViewModel.
45
- *
46
- * Cancelling this job will cancel all coroutines started by this ViewModel.
47
- */
48
- private val viewModelJob = Job ()
49
-
50
- /* *
51
- * This is the scope for all coroutines launched by [PlantDetailViewModel].
52
- *
53
- * Since we pass [viewModelJob], you can cancel all coroutines launched by [viewModelScope] by calling
54
- * viewModelJob.cancel(). This is called in [onCleared].
55
- */
56
- private val viewModelScope = CoroutineScope (Main + viewModelJob)
57
-
58
43
/* *
59
44
* Cancel all coroutines when the ViewModel is cleared.
60
45
*/
46
+ @ExperimentalCoroutinesApi
61
47
override fun onCleared () {
62
48
super .onCleared()
63
- viewModelJob .cancel()
49
+ viewModelScope .cancel()
64
50
}
65
51
66
52
init {
0 commit comments