Last active
July 9, 2019 19:52
-
-
Save navi25/12bfc9ca0a92849ac591ab558dfcc145 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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