【问题标题】:dagger multibinding, Map<java.lang.Integer,? extends javax.inject.Provider<VersionMigration>> cannot be provided without an @Provides-annotated method匕首多重绑定,Map<java.lang.Integer,? extends javax.inject.Provider<VersionMigration>> 不能在没有 @Provides-annotated 方法的情况下提供
【发布时间】:2020-02-02 06:35:21
【问题描述】:

我正在尝试使用 dagger2 实现 Realm 版本管理,通过此链接 Adavis Realm Migration, 但出现以下错误,

[Dagger/MissingBinding] java.util.Map> 不能在没有@Provides-annotated 方法的情况下提供。 公共抽象接口 CoreComponent { ^

代码如下,

interface VersionMigration {
fun migrate(realm: DynamicRealm, oldVersion: Long)
}

class RealmVersion1Migration @Inject constructor(): VersionMigration {

override fun migrate(realm: DynamicRealm, oldVersion: Long) {

    val schema = realm.schema

    if (0 == oldVersion.toInt()) {

        schema.create("RealmAlbumEntity")
            .addField("album_id", String::class.java, FieldAttribute.PRIMARY_KEY)

        Timber.e("migration V1 complete")
    }
}
}

 @Reusable
class ERealmMigration @Inject constructor(
private val versionMigrations:
Map<Int, Provider<VersionMigration>>
) : RealmMigration {

init {
    Timber.e("IN Realm Migration")
}
override fun migrate(
    realm: DynamicRealm,
    oldVersion: Long,
    newVersion: Long
) {
    for (i in oldVersion.toInt() until newVersion) {
        val provider: Provider<VersionMigration> = versionMigrations.getValue(i.toInt())
        val versionMigration = provider.get()
        versionMigration.migrate(realm, i)
    }
}

}

这是我在 realmConfig 中包含的模块, 私有 const val REALM_SCHEMA = 1L

 @Module(includes = [MigrationsModule::class])
 class RealmConfigModule {

@Provides
@AppScope
fun provideRealm(realmConfiguration: RealmConfiguration): Realm {
    return Realm.getInstance(realmConfiguration)
}

@Provides
@AppScope
fun provideRealmConfiguration(
    @Named(CONTEXT_APPLICATION) application: Application,
    realmMigration: ERealmMigration
): RealmConfiguration {

    val config = RealmConfiguration.Builder().name(application.getString(R.string.real_db_name))
        .schemaVersion(REALM_SCHEMA)
        .migration(realmMigration)
        .modules(UserRealmModule(), EventRealmModule())
        .build()

    Realm.setDefaultConfiguration(config)

    return config
}

}

这是我的 AppComponent,

@Component(modules = [RealmConfigModule::class])
@AppScope
interface CoreComponent {

@Component.Factory
interface Factory {
    fun create(@BindsInstance application: Application): CoreComponent
}

}

我试图通过以下链接,但无法从中收集任何有用的内容。 Link 1 Link 2 Link 3

【问题讨论】:

  • 您在 模块 中使用了ERealmMigration,但从未向其提供地图内容。那dagger怎么处理呢?
  • 即使,我在 dagger 中定义它,它仍然给我同样的错误,因为现在它是一个名为 MultiBinding 的功能,由 dagger2 提供

标签: android realm dagger-2 dagger realm-migration


【解决方案1】:

这是一个简单的错误,我只需要添加“@JvmSuppressWildcards”,

 @Reusable
class ERealmMigration @Inject constructor(
private val versionMigrations:
Map<Int,   @JvmSuppressWildcards Provider<VersionMigration>>
) : RealmMigration 

,感谢这个用户的回答,

kotlin + Dagger2 : cannot be provided without an @Provides-annotated method

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-06
    • 2021-05-05
    • 2021-06-11
    • 1970-01-01
    相关资源
    最近更新 更多