DispatchLifecycleScope

open class DispatchLifecycleScope(lifecycle: Lifecycle, coroutineContext: CoroutineContext) : MainImmediateCoroutineScope

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.

  1. If a DispatcherProvider element isn't present, DefaultDispatcherProvider.get will be added.

  2. If a Job element isn't present, a SupervisorJob will be added.

  3. 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

coroutineContext

optional the source CoroutineContext which will be converted to a MainImmediateCoroutineScope. Its Elements will be re-used.

Constructors

Link copied to clipboard
fun DispatchLifecycleScope(lifecycle: Lifecycle, coroutineContext: CoroutineContext = MainImmediateContext())

Types

Link copied to clipboard

Describes the way a particular Job will behave if the lifecycle passes below the minimum state before said Job has completed.

Functions

Link copied to clipboard
fun launchOnCreate(context: CoroutineContext = EmptyCoroutineContext, minimumStatePolicy: DispatchLifecycleScope.MinimumStatePolicy = MinimumStatePolicy.RESTART_EVERY, block: suspend CoroutineScope.() -> Unit): Job

Lifecycle-aware function for launching a coroutine any time the Lifecycle.State is at least Lifecycle.State.CREATED.

Link copied to clipboard
fun launchOnResume(context: CoroutineContext = EmptyCoroutineContext, minimumStatePolicy: DispatchLifecycleScope.MinimumStatePolicy = MinimumStatePolicy.RESTART_EVERY, block: suspend CoroutineScope.() -> Unit): Job

Lifecycle-aware function for launching a coroutine any time the Lifecycle.State is at least Lifecycle.State.RESUMED.

Link copied to clipboard
fun launchOnStart(context: CoroutineContext = EmptyCoroutineContext, minimumStatePolicy: DispatchLifecycleScope.MinimumStatePolicy = MinimumStatePolicy.RESTART_EVERY, block: suspend CoroutineScope.() -> Unit): Job

Lifecycle-aware function for launching a coroutine any time the Lifecycle.State is at least Lifecycle.State.STARTED.

Properties

Link copied to clipboard
open override val coroutineContext: CoroutineContext
Link copied to clipboard
val lifecycle: Lifecycle

the lifecycle to which this MainImmediateCoroutineScope is linked.

Inheritors

Link copied to clipboard

Sources

Link copied to clipboard