LifecycleScopeFactory

object LifecycleScopeFactory

Factory holder for DispatchLifecycleScope's.

By default, create returns a MainImmediateContext.

This factory can be overridden for testing or to include a custom CoroutineContext in production code. This may be done in Application.onCreate, or else as early as possible in the Application's existence.

A custom factory will persist for the application's full lifecycle unless overwritten or reset.

reset may be used to reset the factory to default at any time.

Samples

import dispatch.android.espresso.IdlingDispatcherProvider
import dispatch.android.lifecycle.LifecycleScopeFactory
import dispatch.android.lifecycle.MainImmediateContext
import dispatch.core.DispatcherProvider
import dispatch.internal.test.Application
import dispatch.internal.test.MyCustomElement
import dispatch.internal.test.Sample5
import kotlinx.coroutines.SupervisorJob
fun main() { 
   //sampleStart 
   class MyApplication : Application {

  override fun onCreate() {

    LifecycleScopeFactory.set { MyCustomElement() + MainImmediateContext() }
  }
} 
   //sampleEnd
}
import dispatch.android.espresso.IdlingDispatcherProvider
import dispatch.android.lifecycle.LifecycleScopeFactory
import dispatch.android.lifecycle.MainImmediateContext
import dispatch.core.DispatcherProvider
import dispatch.internal.test.Application
import dispatch.internal.test.MyCustomElement
import dispatch.internal.test.Sample5
import kotlinx.coroutines.SupervisorJob
fun main() { 
   //sampleStart 
   class MyEspressoTest {

  @Before
  fun setUp() {

    val dispatcherProvider = IdlingDispatcherProvider()

    LifecycleScopeFactory.set {
      SupervisorJob() + dispatcherProvider + dispatcherProvider.mainImmediate
    }
  }
} 
   //sampleEnd
}

See also

Functions

Link copied to clipboard
fun reset()

Immediately resets the factory function to its default.

Link copied to clipboard
fun set(factory: DispatchLifecycleScopeFactory)
fun set(factory: () -> CoroutineContext)

Override the default MainImmediateCoroutineScope factory, for testing or to include a custom CoroutineContext in production code. This may be done in Application.onCreate

Sources

Link copied to clipboard