Last active
May 12, 2020 22:15
-
-
Save coroutineDispatcher/50155f87b2391c5e7ed50fd019b407c4 to your computer and use it in GitHub Desktop.
Current Setup ViewModel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SetupViewModel @Inject constructor(private val setupRepository: SetupRepository) : ViewModel() { | |
//CoroutineContext: | |
private val completableJob = Job() | |
private val coroutineScope = CoroutineScope(Dispatchers.IO + completableJob) | |
//Other variables here | |
fun loadListOfCountries() { | |
//Fires in the IO thread. | |
coroutineScope.launch { | |
makeCountriesApiCall() | |
} | |
} | |
//Other methods here | |
override fun onCleared() { | |
super.onCleared() | |
//Canceling a job when the ViewModel is being finished . | |
completableJob.cancel() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment