Referencing Coroutine Dispatchers
within a class makes testing difficult which is why passing the CoroutineDispatcher
via
constructor arguments is a wise decision. component-coroutines
' CoroutineDispatchers
interface serves as a wedge that points to
whatever dispatcher you set it up with.
A full list of kotlin-components
projects can be found HERE
// build.gradle.kts
dependencies {
implementation("io.matthewnelson.kotlin-components:coroutines:1.1.5")
}
// build.gradle
dependencies {
implementation "io.matthewnelson.kotlin-components:coroutines:1.1.5"
}
Note: as of 1.1.0
, the experimental memory model for KotlinNative is enabled.
coroutines | kotlin | kotlinx-coroutines |
---|---|---|
1.1.5 | 1.8.0 | 1.6.4 |
1.1.4 | 1.7.20 | 1.6.4 |
1.1.3 | 1.6.21 | 1.6.3 |
1.1.2 | 1.6.21 | 1.6.1 |
1.1.1 | 1.6.21 | 1.6.1 |
1.1.0 | 1.6.10 | 1.6.0 |
1.0.0 | 1.5.31 | 1.5.2 |
@Module
@InstallIn(SingletonComponent::class)
object AppModule {
@Provides
@Singleton
fun provideCoroutineDispatchers(): CoroutineDispatchers =
object : CoroutineDispatchers {
override val default: CoroutineDispatcher
get() = Dispatchers.Default
override val io: CoroutineDispatcher
get() = Dispatchers.IO
override val main: CoroutineDispatcher
get() = Dispatchers.Main
override val mainImmediate: CoroutineDispatcher
get() = Dispatchers.Main.immediate
override val unconfined: CoroutineDispatcher
get() = Dispatchers.Unconfined
}
}
@HiltViewModel
class MyViewModel @Inject constructor(
dispatchers: CoroutineDispatchers
): ViewModel(), CoroutineDispatchers by dispatchers {
init {
viewModelScope.launch(io) {
// do work
}
}
}
This project utilizes git submodules. You will need to initialize them when cloning the repository via:
$ git clone --recursive https://github.com/05nelsonm/component-coroutines.git
If you've already cloned the repository, run:
$ git checkout master
$ git pull
$ git submodule update --init
In order to keep submodules updated when pulling the latest code, run:
$ git pull --recurse-submodules