-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Comments
As per this guideline, can you please help me with the following example if any changes need to be done?
|
|
Thanks @qwwdfsad , Just to confirm, do you mean like this?
|
This comment has been minimized.
This comment has been minimized.
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. |
@guelo: this is actually provided in a first-party way, it's called |
Well now I feel stupid. I'm not sure how I missed that.
…On Tue, Mar 24, 2020 at 11:25 PM Márton Braun ***@***.***> wrote:
@guelo <https://github.com/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
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1581 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AA4CXC4ZP4AWBZDWYTHEVUDRJGPW5ANCNFSM4I3NHLNQ>
.
|
* 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
* 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
CoroutineScope
documentation currently recommends the following code pattern:It's time to update it to explict scope style, which seems to be winning in its clarity and popularity:
The text was updated successfully, but these errors were encountered: