Cancel children only

Report a typo

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.

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
}
___

Create a free account to access the full topic