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
suspend fun loadMovies() { | |
} |
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
requestScope.launch(Dispatchers.Main) { | |
val moviesComedy = async(Dispatchers.IO) { loadMovies("Comedy") } | |
val moviesAction = async(Dispatchers.IO) { loadMovies("Action") } | |
showMoviesGenres(listOf(moviesComedy.await(), moviesAction.await()) | |
} |
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
requestScope.launch(Dispatchers.Main) { | |
val moviesComedy = async(Dispatchers.IO) { loadMovies("Comedy") }.await() | |
val moviesAction = async(Dispatchers.IO) { loadMovies("Action") }.await() | |
showMoviesGenre(moviesComedy) | |
showMoviesGenre(moviesAction) | |
} |
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
requestScope.launch(Dispatchers.Main) { | |
val moviesComedy = withContext(Dispatchers.IO) { loadMovies("Comedy") } | |
val moviesAction = withContext(Dispatchers.IO) { loadMovies("Action") } | |
showMoviesGenre(moviesComedy) | |
showMoviesGenre(moviesAction) | |
} |
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
requestScope.launch { | |
val moviesComedy = loadMovies("Comedy") | |
val moviesAction = loadMovies("Action") | |
launch(Dispatchers.Main) { | |
showMoviesGenre(moviesComedy) | |
showMoviesGenre(moviesAction) | |
} | |
} |
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
val value : Deferred<Int> = requestScope.async { | |
42 | |
} |
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
requestScope.async { | |
} |
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
fun loadMovies() { | |
mainScope.launch { | |
val movies = withContext(Dispatchers.IO) { // launch a coroutine in another Dispatcher and return the result | |
loadMovies() | |
} | |
showMovies(movies) | |
} | |
} |
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
fun loadMovies() { | |
backgroundScope.launch { | |
val movies = loadMovies() // load the movies on the background thread | |
launch(Dispatchers.Main) { // to touch the views to show the movies, we need to launch another coroutine in the main thread | |
showMovies(movies) | |
} | |
} | |
} |
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
val launchJob = mathScope.launch { | |
} | |
launchJob.cancel() |