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

v0ldem0rt

12/07/2020, 2:43 AM
Is there a way to register a cancellation listener on a coroutine scope without doing
suspendCancellableCoroutine
so that if the scope gets cancelled I can close my DB statements
b

bezrukov

12/07/2020, 5:23 AM
You can use try-finally/`.use` ext to close resources. But if you need a scope callback - you can take Job element from scope and then use invokeOnClose
v

v0ldem0rt

12/07/2020, 1:19 PM
I ended up spinning up a new coroutine in same scope and then hooked up
suspendCancellableCoroutine
b

bezrukov

12/07/2020, 1:34 PM
if scope was cancelled before you launched it - your resource will leak, because coroutine won't be even launched (for this case you need to use non-default
start
option - either Atomic or Undispatched)
3 Views