2017-12-31 111 views
0

나는 app 구성 요소와 종속 구성 요소가 있습니다. app 구성 요소는 명시 적 종속성을 선언하고 종속 구성 요소는 명시 적 종속성을 주입 할 수 있습니다. 그러나 @Qualifier를 사용해야하는 종속성이있을 때 종속 구성 요소는 종속성을 주입 할 수 없습니다. 이것은 규정 자입니다Dagger2 및 종속 구성 요소의 한정자

@Module 
class SchedulersModule { 
    @ApplicationScope 
    @Provides 
    @IoScheduler 
    fun provideIoScheduler(): Scheduler = Schedulers.io() 

    @ApplicationScope 
    @Provides 
    @UiScheduler 
    fun provideMainThreadScheduler(): Scheduler = AndroidSchedulers.mainThread() 
} 

:

이 스케줄러 모듈입니다

@Component(modules = [AppModule::class, SchedulersModule::class, StorageModule::class]) 

@ApplicationScope 
interface AppComponent { 
    fun inject(app: Application) 
    /* other stuff omitted for brevity */ 
    val bitmapCache: BitmapCache   
    @UiScheduler fun uiScheduler(): Scheduler 
} 

응용 프로그램 구성 요소입니다

@Qualifier 
@Retention(AnnotationRetention.RUNTIME) 
annotation class UiScheduler 

그리고 이것은 종속 구성 요소입니다

UI가 스케줄러를

class EditEntryFragment : Fragment() { 
    @Inject @UiScheduler lateinit var uiScheduler: Scheduler 
    /* other stuff */ 
} 

가 왜 종속 구성 요소가 상위 구성 요소에 선언 된 비트 맵 캐시를 삽입 할 수 있지만 :

이 스케줄러가 조각에 주입하는 방법이다?

error: io.reactivex.Scheduler cannot be provided without an @Provides- or @Produces-annotated method. 
    io.reactivex.Scheduler is injected at 
     com.test.edit.EditEntryFragment.uiScheduler 
    com.test.edit.EditEntryFragment is injected at 
     com.test.edit.EditEntryActivityComponent.inject(arg0) 
1 error 

답변

1

@field 사용 : 클래스 EditEntryFragment에

0

시도 @Named annotition

@Inject @field:Named("UiScheduler") lateinit var uiScheduler: Scheduler 

체크 아웃 this issue

+0

이 구문이 올바르지 않습니다 (당신이 누락을 UiScheduler를이 내가 오류입니다 UiScheduler의 인용문). 그러나 관계없이, 이것은'@ Named' 주석을위한 것이며, 현재 한정어를 사용하고 있습니다. 같은 해결책이 적용되지 않는다. 적어도 적어도 내가 말할 수있는 한 똑같은 방식은 아니다. '@ 한정자 '를 사용하는 의존성에 대한이'@ 필드'수정의 변형이 있습니까? – Francesc

+0

잘 모르겠습니다. 어쩌면 당신은 @Named를 시도해 볼 수 있습니다. – Garywzh

+0

고마워요,하지만 문제는 한정어에만 해당됩니다. – Francesc

관련 문제