DispatchLifecycleScope
MainImmediateCoroutineScope which is tied to a Lifecycle.
The CoroutineScope provides lifecycle-aware launch functions which will automatically start upon reaching their associated Lifecycle.Event, then automatically cancel upon the lifecycle dropping below that state. Reaching that state again will start a new Job.
This CoroutineScope
's Job will be cancelled automatically as soon as the lifecycle
reaches DESTROYED.
If a DispatcherProvider element isn't present, DefaultDispatcherProvider.get will be added.
If a Job element isn't present, a SupervisorJob will be added.
If the ContinuationInterceptor does not match the one referenced by the possibly new property, it will be updated to match.
Samples
import dispatch.android.lifecycle.DispatchLifecycleScope
import dispatch.android.lifecycle.DispatchLifecycleScope.MinimumStatePolicy.CANCEL
import dispatch.android.lifecycle.dispatchLifecycleScope
import dispatch.core.DispatcherProvider
import dispatch.core.launchMain
import dispatch.internal.test.Sample5
import dispatch.internal.test.android.LiveDataTest
import dispatch.test.CoroutineTest
import io.kotest.matchers.shouldBe
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.runBlocking
fun main() {
//sampleStart
runBlocking {
// This could be any LifecycleOwner -- Fragments, Activities, Services...
class SomeFragment : Fragment() {
val lifecycleScope = DispatchLifecycleScope(lifecycle)
init {
// active only when "resumed". starts a fresh coroutine each time
lifecycleScope.launchOnResume { }
// active only when "started". starts a fresh coroutine each time
// this is a rough proxy for LiveData behavior
lifecycleScope.launchOnStart { }
// active after only the first "started" event, and never re-started
lifecycleScope.launchOnStart(minimumStatePolicy = CANCEL) { }
// launch when created, automatically stop on destroy
lifecycleScope.launchOnCreate { }
// it works as a normal CoroutineScope as well (because it is)
lifecycleScope.launchMain { }
}
}
}
//sampleEnd
}
import dispatch.android.lifecycle.DispatchLifecycleScope
import dispatch.android.lifecycle.DispatchLifecycleScope.MinimumStatePolicy.CANCEL
import dispatch.android.lifecycle.dispatchLifecycleScope
import dispatch.core.DispatcherProvider
import dispatch.core.launchMain
import dispatch.internal.test.Sample5
import dispatch.internal.test.android.LiveDataTest
import dispatch.test.CoroutineTest
import io.kotest.matchers.shouldBe
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.runBlocking
fun main() {
//sampleStart
runBlocking {
// This could be any LifecycleOwner -- Fragments, Activities, Services...
class SomeFragment : Fragment() {
val context = Job() + DispatcherProvider()
val lifecycleScope = DispatchLifecycleScope(lifecycle, context)
init {
// active only when "resumed". starts a fresh coroutine each time
lifecycleScope.launchOnResume { }
// active only when "started". starts a fresh coroutine each time
// this is a rough proxy for LiveData behavior
lifecycleScope.launchOnStart { }
// active after only the first "started" event, and never re-started
lifecycleScope.launchOnStart(minimumStatePolicy = CANCEL) { }
// launch when created, automatically stop on destroy
lifecycleScope.launchOnCreate { }
// it works as a normal CoroutineScope as well (because it is)
lifecycleScope.launchMain { }
}
}
}
//sampleEnd
}
Parameters
optional the source CoroutineContext which will be converted to a MainImmediateCoroutineScope. Its Elements will be re-used.
Constructors
Types
Describes the way a particular Job will behave if the lifecycle passes below the minimum state before said Job has completed.
Functions
Lifecycle-aware function for launching a coroutine any time the Lifecycle.State is at least Lifecycle.State.CREATED.
Lifecycle-aware function for launching a coroutine any time the Lifecycle.State is at least Lifecycle.State.RESUMED.
Lifecycle-aware function for launching a coroutine any time the Lifecycle.State is at least Lifecycle.State.STARTED.
Properties
the lifecycle to which this MainImmediateCoroutineScope is linked.