question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CoroutineScope.cancel() extension to reduce boiler-plate in a typical UI app

See original GitHub issue

I’m reading Android codelabs on coroutines here https://codelabs.developers.google.com/codelabs/kotlin-coroutines/index.html#4

It gave me a fresh look on the pattern we are advertising for CoroutineScope. There it is being cast without inheritance:

private val viewModelJob = Job()
private val uiScope = CoroutineScope(Dispatchers.Main + viewModelJob)

override fun onCleared() {
    super.onCleared()
    viewModelJob.cancel()
}

This does look like bit a boilerplate, even if you have to write it once in your app. What we can do, is to have a cancel() extension function directly on CoroutineScope.

PRO: It is going to simplify the above code (note that CoroutineScope(...) constructor always creates a Job object):

private val uiScope = CoroutineScope(Dispatchers.Main)

override fun onCleared() {
    super.onCleared()
    uiScope.cancel()
}

CON: We have a convention of declaring “coroutine builder” functions as extensions on CoroutineScope. It mean, that if we have cancel() extension on CoroutineScope, then it will become available as cancel() in the scope of those coroutine builder functions, which could be confusing and is completely useless (at least, I don’t see a use-case for cancel() in the scope of coroutine builder). I don’t have a nice solution for this downside.

Any comments?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
elizarovcommented, Oct 8, 2018

@objcode CancellableCoroutineScope can be an inline class, indeed. However, it kind of misnomer, since every instance of CoroutineScope should be cancellable, since we already have code to create a Job() in a regular CoroutineScope(...) constructor function.

I’ve looked at the usage via coroutineContext that you’ve proposed and it already seems quite good for me. Maybe we shall just update codelab to use this pattern and stop trying to further improve it:

private val uiScope = CoroutineScope(Dispatchers.Main)

override fun onCleared() {
    super.onCleared()
    uiScope.coroutineContext.cancel() // is it confusing or Ok? 
}
0reactions
elizarovcommented, Oct 8, 2018

LGTM. I’ll close this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Easy Coroutines in Android: viewModelScope - Medium
Cancelling coroutines when they are no longer needed can be a task easy to forget, it's monotonous work and adds a lot of...
Read more >
Eliminating Coroutine leaks in tests | by Rick Busarow
We demonstrated how a coroutine can leak in tests, and how to stop that bleeding with scope.cancel() . We then created a Rule...
Read more >
Use Kotlin Coroutines in your Android App - Android Developers
Coroutines are a Kotlin feature that converts async callbacks for long-running tasks, such as database or network access, into sequential code.
Read more >
Kotlin Coroutines: Let it async in - Codementor
As we've seen, Kotlin Coroutines are an easy way to write asynchronous, non-blocking code. If used properly, we can reduce the boilerplate code ......
Read more >
Clean Android multi-module offline-first scalable app in 2022
Nevertheless, we can use it for standard Android apps as well. ... terms of significantly reduced number of UI bugs in production apps...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found