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.

Update CoroutineScope docs to recommend explicit scope

See original GitHub issue

CoroutineScope documentation currently recommends the following code pattern:

class MyActivity : AppCompatActivity(), CoroutineScope by MainScope() {
    ...
}

It’s time to update it to explict scope style, which seems to be winning in its clarity and popularity:

class MyActivity : AppCompatActivity() {
    private val scope = MainScope() 
    ...
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:17
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
kpritamcommented, Sep 30, 2019

@kpritam

class Script : CoroutineScope {
    private val executor = ... // Separate variable to close it later
    private val scriptScope = CoroutineScope(executor)
}

Thanks @qwwdfsad , Just to confirm, do you mean like this?

class Script {
    private val executor = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
    private val scriptScope = CoroutineScope(executor)
    init {
        scriptScope.launch { delay(1000) }
    }
}
1reaction
kpritamcommented, Sep 29, 2019

As per this guideline, can you please help me with the following example if any changes need to be done?

class Script : CoroutineScope {
    private val executor = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
    private val job = Job()

    override val coroutineContext: CoroutineContext get() = job + executor
    
    // ....
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Use Kotlin coroutines with lifecycle-aware components
A LifecycleScope is defined for each Lifecycle object. Any coroutine launched in this scope is canceled when the Lifecycle is destroyed. You can ......
Read more >
Coroutine Context and Scope - Roman Elizarov - Medium
By convention, a context in CoroutineScope contains a Job that is going to become a parent of new coroutine (with the exception of...
Read more >
Coroutine context and dispatchers - Kotlin
All coroutine builders like launch and async accept an optional CoroutineContext parameter that can be used to explicitly specify the dispatcher ...
Read more >
Kotlin Coroutines 1.5: GlobalScope Marked as Delicate ...
A global CoroutineScope is not bound to any job. Global scope is used to launch top-level coroutines that operate during the whole application ......
Read more >
How to return value from coroutine scope - Stack Overflow
If you need to run works in parallel then you can use coroutineScope or async coroutine builder functions. However, in your case you...
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