Our code loads all required data and also starts a child coroutine to do some caching. If the user leaves the screen, we don't want to continue that caching work. Let's cancel it, but without stopping all other work going on in the coroutine.
Structured Concurrency & Cancellation
Cancel children only
Report a typo
Write a program in Kotlin
private suspend fun CoroutineScope.loadScreen() {
// launch pre-caching in background
launch { preCache("image_3") }
// load primary data
loadImage("image_1")
loadImage("image_2")
}
// this function will be called right after pre-caching starts
private fun stopLoading(scope: CoroutineScope) {
// insert code here
}
___
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.