Skip to content

Instantly share code, notes, and snippets.

@coroutineDispatcher
Last active May 12, 2020 22:15
Show Gist options
  • Save coroutineDispatcher/50155f87b2391c5e7ed50fd019b407c4 to your computer and use it in GitHub Desktop.
Save coroutineDispatcher/50155f87b2391c5e7ed50fd019b407c4 to your computer and use it in GitHub Desktop.
Current Setup ViewModel
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