Skip to content

Instantly share code, notes, and snippets.

@navi25
Last active July 9, 2019 19:52
Show Gist options
  • Save navi25/12bfc9ca0a92849ac591ab558dfcc145 to your computer and use it in GitHub Desktop.
Save navi25/12bfc9ca0a92849ac591ab558dfcc145 to your computer and use it in GitHub Desktop.
class TmdbViewModel : ViewModel(){
private val parentJob = Job()
private val coroutineContext: CoroutineContext
get() = parentJob + Dispatchers.Default
private val scope = CoroutineScope(coroutineContext)
private val repository : MovieRepository = MovieRepository(ApiFactory.tmdbApi)
val popularMoviesLiveData = MutableLiveData<MutableList<TmdbMovie>>()
fun fetchMovies(){
scope.launch {
val popularMovies = repository.getPopularMovies()
popularMoviesLiveData.postValue(popularMovies)
}
}
fun cancelAllRequests() = coroutineContext.cancel()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment