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()
...
}
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
// ....
}
@kpritam
class Script : CoroutineScope {
private val executor = ... // Separate variable to close it later
private val scriptScope = CoroutineScope(executor)
}
@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) }
}
}
ทุกอย่างที่เกิดขึ้นจากการกระทำของทั้งหมด ผมยังไม่เข้าภาษาตัวต่อ ที่ผมเรียนได้ก็พวกคุณทั้งหมด ผมขอโอกาศ กับพวกเราทั้งได้ไหมคับหากว่าผมตัดสินใจไปแล้วผิดขอให้ทุกคนยกโทษให้ผมด้วยนะคับ
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 LifecycleScope, and you can access it on any LifecycleOwner (Activity, Fragment, Fragment view owner): https://developer.android.com/topic/libraries/architecture/coroutines#lifecyclescope
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 notifications@github.com
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
https://github.com/Kotlin/kotlinx.coroutines/issues/1581#issuecomment-603664174,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AA4CXC4ZP4AWBZDWYTHEVUDRJGPW5ANCNFSM4I3NHLNQ
.
Most helpful comment
Thanks @qwwdfsad , Just to confirm, do you mean like this?