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 makeLogin(login: String, password: String, callback: (Token) -> Unit) { | |
// request login | |
callback(token) | |
} | |
suspend fun loadMovies(callback: (List<Movie>) -> Unit) { | |
makeLogin("someValue", "someValue") { token -> | |
// request movies with token | |
callback(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
suspend fun makeLogin(login: String, password: String) : Token { | |
// request login | |
return token | |
} | |
suspend fun loadMovies(token: Token) : List<Movie> { | |
val token = makeLogin("someValue", "someValue") | |
// request movies with token | |
return 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
mathScope.launch { | |
// calculate PI | |
} |
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
mathScope.cancel() |
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
mathScope.launch { | |
if(mathScope.ensureActive()) { // ensures that the scope is active and if not, it will throw a exception. | |
// continue the calculation | |
} | |
if(mathScope.isActive) { // returns true or false if the scope is active or not | |
// continue the calculation | |
} | |
} |
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 mathScope = CoroutineScope(Job() + Dispatchers.IO) |
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
mathScope.launch(Job()) { | |
} |
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() |
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
fun loadMovies() { | |
mainScope.launch { | |
val movies = withContext(Dispatchers.IO) { // launch a coroutine in another Dispatcher and return the result | |
loadMovies() | |
} | |
showMovies(movies) | |
} | |
} |