ViewModelScopeFactory

object ViewModelScopeFactory

Factory holder for viewModelScope's.

By default, create returns a MainImmediateCoroutineScope, but may return any CoroutineScope.

This factory can be overridden for testing or to include a custom CoroutineContext in production code. This may be done in Application.onCreate.

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

Samples

import dispatch.android.espresso.MainImmediateIdlingCoroutineScope
import dispatch.android.viewmodel.ViewModelScopeFactory
import dispatch.core.MainImmediateCoroutineScope
import dispatch.internal.test.Sample5
import dispatch.test.TestProvidedCoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
fun main() { 
   //sampleStart 
   class MyApplication : Application {

  override fun onCreate() {
    ViewModelScopeFactory.set { MainImmediateCoroutineScope() }
  }
} 
   //sampleEnd
}
import dispatch.android.espresso.MainImmediateIdlingCoroutineScope
import dispatch.android.viewmodel.ViewModelScopeFactory
import dispatch.core.MainImmediateCoroutineScope
import dispatch.internal.test.Sample5
import dispatch.test.TestProvidedCoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
fun main() { 
   //sampleStart 
   class MyEspressoTest {

  @Before
  fun setUp() {
    ViewModelScopeFactory.set { MainImmediateIdlingCoroutineScope() }
  }
} 
   //sampleEnd
}
import dispatch.android.espresso.MainImmediateIdlingCoroutineScope
import dispatch.android.viewmodel.ViewModelScopeFactory
import dispatch.core.MainImmediateCoroutineScope
import dispatch.internal.test.Sample5
import dispatch.test.TestProvidedCoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
fun main() { 
   //sampleStart 
   class MyJvmTest {

  @Before
  fun setUp() {
    ViewModelScopeFactory.set { TestProvidedCoroutineScope() }
  }
} 
   //sampleEnd
}

Functions

Link copied to clipboard
fun reset()

Immediately resets the factory function to its default.

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

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