coroutines

Module that provides access to different CoroutineDispatchers and CoroutineScopes 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
}

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"))
}

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
}
}
}

Packages

Link copied to clipboard
Link copied to clipboard