-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Open
Labels
Description
I want to use CoroutineScope
that is only working when two Job
s are active.
Example
class Something : CoroutineScope {
// onBind ~ onUnbind
private var scopeJobBind = Job().apply { cancel() }
// onResume ~ onPause
private var scopeJobLifecycle = Job().apply { cancel() }
// Intention: (onBind ~ onUnbind) ∩ (onResume ~ onPause)
override val coroutineContext get() = Dispatchers.Main + scopeJobBind + scopeJobLifecycle // The problem: scopeJobLifecycle replaces scopeJobBind
fun onBind() {
scopeJobBind = Job()
}
fun onUnbind() {
scopeJobBind.cancel()
}
fun onResume() {
scopeJobLifecycle = Job()
}
fun onPause() {
scopeJobLifecycle.cancel()
}
fun foo() {
// It should be launched only when both scopeJobBind and scopeJobLifecycle are active.
launch {
...
}
}
}