https://kotlinlang.org logo
#coroutines
Title
# coroutines
l

Lukas Lechner

06/21/2022, 5:30 PM
I created a new video about "Best Practices for using Kotlin Coroutines in Android Development". Maybe its helpful for someone!

https://youtu.be/tVDCpjqQ1Ro

🙌 3
j

Joost Klitsie

06/22/2022, 7:38 AM
cool video! Luckily I am not surprised by most of it 😄 One question: In the best practice 4, you mention that you can run work in the
applicationScope
as to make it finish, even if the original calling scope is cancelled. is the `async`/`await` the only way to do this? Or are there also other ways?
I ask because Android studio really wants me to turn the `async`/`await` into a
withContext(applicationScope.coroutineContext) {..}
which doesn't really do the same, as if you do `withContext`and you cancel during it, then the code afterwards will still run and you'd have to do an
isActive
check:
Copy code
suspend fun CoroutineScope.test() {
    val result = withContext(applicationScope.coroutineContext) {
        delay(200)
        println("producing 5")
        5
    }
    if (!isActive) { return }
    println(result)
}
s

Stylianos Gakis

06/28/2022, 3:02 PM
Having a function which is both suspending and an extension on
CoroutineScope
does look a bit suspicious does it not? In general if a function is an extension on
CoroutineScope
I expect it to be a fire and continue, just like
launch
does. Like mentioned here. And this important quote from the Explicit Concurrency post from Elizarov: “It leads to the following useful convention: every function that is declared as extension on
CoroutineScope
returns immediately, but performs its actions concurrently with the rest of the program.” What are you trying to achieve there Joost, I am a bit confused.
j

Joost Klitsie

06/28/2022, 3:11 PM
@Stylianos Gakis I tried to do a thing that was mentioned in the video, specifically best practice number 4 (using async/await with a differen scope to make work finish even if the original scope gets cancelled). But android studio is giving me a suggestion when I do that to change it to a withContext instead. So my question would be: is this the best way to achieve this behaviour or is there a better way?
s

Stylianos Gakis

06/28/2022, 3:15 PM
And why do you need it to be an extension on CoroutineScope to do that? In the video I see that a CoroutineScope context is introduced by using the
coroutineScope {}
function, maybe this is what you wanted to use instead? I may be misunderstanding something, if yes you could show what you would want your code to look like which doesn’t compile and we can take it from there.
j

Joost Klitsie

06/28/2022, 3:26 PM
It is a test sample
So it compiles and runs
I don't have it as actual code :) but the general idea would be: what is the best way to finish work started from a coroutine when the original scope gets cancelled?
Like I load something from the UI, I start a network call, during this my ui moves away and kills its scope, but I still want to finish this backend call.
s

Stylianos Gakis

06/28/2022, 3:39 PM
If you care for the response, I guess you need to do the async approach as shown in the video. If you do not you can simply do
val job = appliacationScope.launch{stuff here}; job.join()
or not even do the join part if you don’t care for waiting for it to finish. I don’t know if you can do this without async if you want the response. If someone knows I’d love to see it.
j

Joost Klitsie

06/29/2022, 12:09 PM
cool 🙂 thanks btw for the info and the links you shared!
🙌 1
7 Views