0

For Injecting Coroutines Dispatchers while we have Hilt or Dagger as DI framework, we can define annotations to differ between IO, Default and Main Dispatchers :

@Module
@InstallIn(SingletonComponent::class)
object DispatcherModule {

    @IoDispatcher
    @Provides
    fun providesIoDispatcher(): CoroutineDispatcher = Dispatchers.IO
}

@Retention(AnnotationRetention.BINARY)
@Qualifier
annotation class IoDispatcher

How can we differ between IO, Default and Main Dispatchers when Using Koin? We can have following in Koin as far as I know :

val ioDispatcherModule = module {

    single { Dispatchers.IO }
}
Ali
  • 9,800
  • 19
  • 72
  • 152
  • Not sure but Probably with Scope with a named parameter i guess . https://insert-koin.io/docs/reference/koin-core/scopes – ADM Dec 13 '21 at 08:32
  • As I understand we need to Specify the class in Scope. Such as scope { Scoped { Dispatchers.IO } } . Does it mean that we need to define a module for every repository that we use Dispatchers in it? – Ali Dec 13 '21 at 08:48
  • 1
    Sorry i haven't used Koin intensively . I think you can do it [This way](https://stackoverflow.com/a/56630047/4168607) . you can create `named` injection for all 3 dispatchers . – ADM Dec 13 '21 at 10:40

0 Answers0