Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update CoroutineScope docs to recommend explicit scope #1581

Closed
elizarov opened this issue Sep 28, 2019 · 7 comments
Closed

Update CoroutineScope docs to recommend explicit scope #1581

elizarov opened this issue Sep 28, 2019 · 7 comments
Labels
docs KDoc and API reference

Comments

@elizarov
Copy link
Contributor

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() 
    ...
}
@elizarov elizarov added the docs KDoc and API reference label Sep 28, 2019
@kpritam
Copy link

kpritam commented 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
    
    // ....
}

@qwwdfsad
Copy link
Member

qwwdfsad commented Sep 30, 2019

@kpritam

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

@kpritam
Copy link

kpritam commented 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) }
    }
}

@Wanchan9

This comment has been minimized.

@guelo
Copy link

guelo commented Mar 25, 2020

Not sure if it matters for the documentation, but in my team instead of doing something like this

class Activity {
    private val mainScope = MainScope()
    
    fun destroy() {
        mainScope.cancel()
    }
}

we created a class that ties the lifecycles and scope together using androidx.lifecycle observer.

class LifecycleScope(lifecycleOwner: LifecycleOwner) : CoroutineScope, LifecycleObserver {

    init {
        lifecycleOwner.lifecycle.addObserver(this)
    }

    override val coroutineContext = SupervisorJob() + Dispatchers.Main

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    fun destroy() = coroutineContext.cancel()
}

Then we don't have to worry about managing the scope cancellation in every activity

class Activity: FragmentActivity() {
    private val scope = LifecycleScope(this)
}

I suspect the android team didn't create something like this because they're trying to guide people to keep all coroutine stuff in their ViewModels and to only use LiveData in Activitys and Fragments. But I find it's sometimes still useful to use coroutines in the Activitys and Fragments.

@zsmb13
Copy link
Contributor

zsmb13 commented Mar 25, 2020

@guelo: this is actually provided in a first-party way, it's called LifecycleScope, and you can access it on any LifecycleOwner (Activity, Fragment, Fragment view owner): https://developer.android.com/topic/libraries/architecture/coroutines#lifecyclescope

elizarov added a commit that referenced this issue Mar 25, 2020
@guelo
Copy link

guelo commented Mar 25, 2020 via email

qwwdfsad pushed a commit that referenced this issue Apr 24, 2020
* Update CoroutineScope docs
* Fixed scope examples in guides, added notes on first-party support in Android.
* Simplified scopes section in UI guide since it is mostly irrelevant.

Fixes #1581
recheej pushed a commit to recheej/kotlinx.coroutines that referenced this issue Dec 28, 2020
* Update CoroutineScope docs
* Fixed scope examples in guides, added notes on first-party support in Android.
* Simplified scopes section in UI guide since it is mostly irrelevant.

Fixes Kotlin#1581
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs KDoc and API reference
Projects
None yet
Development

No branches or pull requests

6 participants