coroutines
Module that provides access to different CoroutineDispatcher
s and CoroutineScope
s in coroutine-based components to manage concurrency and threading.
Access is enabled via DispatchersProvider
component which is available in Hilt DI graph:
interface DispatchersProvider {
fun main(): CoroutineDispatcher
fun default(): CoroutineDispatcher
fun io(): CoroutineDispatcher
fun unconfined(): CoroutineDispatcher
}
Content copied to clipboard
It additionally provides four qualifiers to request the injection of a specific CoroutineDispatcher
or CoroutineScope
as shown in #example-usage.
Dependency
dependencies {
implementation(project(":core:coroutines"))
}
Content copied to clipboard
Example usage
class MyClass @Inject constructor(
private val dispatchersProvider: DispatchersProvider,
@Dispatching.IO private val ioDispatcher: CoroutineDispatcher,
@Dispatching.Main private val mainScope: CoroutineScope,
) {
suspend fun executeWithDispatcher() = withContext(ioDispatcher) {
// Perform IO-bound operation here
}
suspend fun executeWithProvider() = withContext(dispatchersProvider.default()) {
// Perform operation using a dispatcher from the provider
}
fun launchScope() {
mainScope.launch {
// Perform UI-related task here
}
}
}
Content copied to clipboard