Dagger2 Inherited subcomponent multibindings

走远了吗. 提交于 2019-12-13 03:48:32

问题


Hope to find some help here after days and days researching about this very interested subject "Inherited subcomponent multibindings which you can find here Inherited subcomponent multibindings which is the last subject in that page.

According to the official documentation:

subComponent can add elements to multibound sets or maps that are bound in its parent. When that happens, the set or map is different depending on where it is injected. When it is injected into a binding defined on the subcomponent, then it has the values or entries defined by the subcomponent’s multibindings as well as those defined by the parent component’s multibindings. When it is injected into a binding defined on the parent component, it has only the values or entries defined there.

In other words. If the parent Component has a multibound set or map and a child component has binding to that multibound, then those binding will be link/added into the parent map depending where those binding are injected within the dagger scope if any.

Here is the issue.

Using dagger version 2.24 in an Android Application using Kotlin. I have an ApplicationComponent making use of the new @Component.Factory approach. The ApplicationComponent has installed the AndroidSupportInjectionModule.

I also have an ActivitySubComponent using the new @Component.Factory approach and this one is linked to the AppComponent using the subComponents argument of a Module annotation. This ActivitySubComponent provides a ViewModel thru a binding like this

@Binds
@IntoMap
@ViewModelKey(MyViewModel::class)
fun provideMyViewModel(impl: MyViewModel): ViewModel

the @ViewModelKey is a custom Dagger Annotation.

I also have a ViewModelFactory implemented like this.

@Singleton
class ViewModelFactory @Inject constructor(
    private val viewModelsToInject: Map<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>
) : ViewModelProvider.Factory {

    override fun <T : ViewModel?> create(modelClass: Class<T>): T = 
    viewModelsToInject[modelClass]?.get() as T
}

A normal ViewModelFactory

The difference here is that I am providing this ViewModelFactory in one of the AppComponents modules. But the bind viewModels within the ActivitySubComponent are not getting added into the ViewModelFactory Map in the AppComponent.

In other words. What the documentation is describing is not happening at all.

If I move the viewModels binding into any of the AppComponent Modules, then all work.

Do you know what could be happening here.


回答1:


You're scoping your ViewModelProvider.Factory as @Singleton. This ensures that it will be created and kept within the @Singleton component.

It's safe to remove the scope since it doesn't keep any state, and it would allow the factory to be created where needed with the correct set of bindings.



来源:https://stackoverflow.com/questions/57633347/dagger2-inherited-subcomponent-multibindings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!