我遇到了奇怪的问题。我需要注入两个调度程序。对于ui和异步操作。我有RxModule类:
@Module
class RxModule {
@Named(Keys.SCHEDULER_UI)
@Provides
@Singleton
fun provideUiScheduler(): Scheduler = AndroidSchedulers.mainThread()
@Named(Keys.SCHEDULER_ASYNC)
@Provides
@Singleton
fun provideAsyncScheduler(): Scheduler = Schedulers.io()
}
然后我把它们注入我的演示者
@Named(Keys.SCHEDULER_UI)
@Inject
lateinit var uiScheduler: Scheduler
@Named(Keys.SCHEDULER_ASYNC)
@Inject
lateinit var asyncScheduler: Scheduler
但是Dagger却遭遇了例外:
io.reactivex.Scheduler cannot be provided without an @Provides- or @Produces-annotated method
注射看起来很简单:
ApplicationLoader.instance.getApplicationComponent().inject(presenter)
我的AppComponent:
@Component(modules = [RxModule::class, ApplicationModule::class, WebModule::class, RepositoryModule::class])
@Singleton
interface AppComponent {
fun inject(presenter: MainPresenter)
fun inject(presenter: AppDetailsPresenter)
}
有人遇到过同样的问题吗?提前致谢
因此,经过一段时间的研究,我找到了解决方案。在使用@Named时使用@Inject在Kotlin中你应该以其他方式执行此操作。你注入应该是这样的:
@field:[Inject Named(Keys.SCHEDULER_UI)]
lateinit var uiScheduler: Scheduler
@field:[Inject Named(Keys.SCHEDULER_ASYNC)]
lateinit var asyncScheduler: Scheduler