https://kotlinlang.org logo
Title
p

Pablo

02/02/2021, 9:40 AM
Is this any reason to use
return suspendCoroutine{
   continuation -> usecase.invoke{result -> continuation.resumeWith(Result.success(result)}
}
Is there any difference between suspendCancellableCoroutine? Becuase I know I've been using this for instance for firebase sign in
g

gildor

02/02/2021, 9:57 AM
suspendCancellableCoroutine supports cancellation, and it’s very good idea to support it if API supports it
☝️ 2
also if you use it for Firebase Sign In I believe you writing adapter for Tasks API? If so, such adapter already exist and officially supported by Kotlinx.Coroutines: https://github.com/Kotlin/kotlinx.coroutines/tree/master/integration/kotlinx-coroutines-play-services
p

Pablo

02/02/2021, 10:02 AM
Nono I mean the example in code is not about firebase it's another example that I have, but wondering if I do not want to cancell it that's why this code has the suspendCoroutine right?
g

gildor

02/02/2021, 10:14 AM
I use cancellable version everywhere
if I do not want to cancell
Sounds strange, you don’t want or there is no cancel API?
p

Pablo

02/02/2021, 11:45 AM
Could you give me a reason to cancel it? I mean, for instance if the activity/fragment gets destroyed?
g

gildor

02/02/2021, 1:17 PM
Any reason, bind it to lifecycle is one of them
Every async operation should be cancellable
2
a

Adam Powell

02/02/2021, 3:38 PM
There are two reasons to support cancellation in suspend functions you provide to others: 1) to let your callers signal your code to stop doing work, and 2) to let your callers stop waiting for your code to complete because they no longer care and need to run their own cleanup code, e.g. finally blocks. You might not care about (1) but your callers always care about (2).
🙌🏽 1