【问题标题】:Dager Hilt with Realm database带有 Realm 数据库的 Dagger Hilt
【发布时间】:2021-08-25 14:02:39
【问题描述】:

有人在 Realm 数据库中使用过 Dagger Hilt。我遇到初始化问题。也许上面的解决方案会起作用。

错误->

需要由 Hilt 实例化的模块必须有一个可见的空构造函数。 [Hilt] 处理未完成。有关详细信息,请参阅上面的错误。 任务 ':app:kaptDebugKotlin' 执行失败。 执行 org.jetbrains.kotlin.gradle.internal.KaptExecution 时发生故障 java.lang.reflect.InvocationTargetException(没有错误信息)`

我的模块->

@Module
@InstallIn(SingletonComponent::class)
class LocalDataSourceModule(context: Context) {
    
init {
        Realm.init(context)
    }

    @Provides
    fun provideLocalDataSource(realm: Realm): LocalDataSource = LocalDataSourceImpl(realm)

    @Singleton
    @Provides
    fun provideRealm(configuration: RealmConfiguration): Realm =
        try {
            Realm.getDefaultInstance()
        } catch (e: Exception) {
            Realm.getInstance(configuration)
        }

    @Singleton
    @Provides
    fun provideRealmConfiguration(): RealmConfiguration = RealmConfiguration.Builder().build()
}

我的应用程序->

@HiltAndroidApp
class BaseApplication : Application() {

    lateinit var girlComponent: GirlComponent
        private set

    override fun onCreate() {
        super.onCreate()
        Timber.plant(Timber.DebugTree())

        girlComponent = DaggerGirlComponent.builder().localDataSourceModule(
            LocalDataSourceModule(applicationContext)
        ).build()

    }
}

【问题讨论】:

  • 从未使用过 Hilt,但错误是说您的模块没有无参数构造函数,它没有。在模块中删除构造函数参数,并在其他地方或provideRealm 函数中通过传递Context 作为参数并返回Realm,就像您创建的任何其他依赖项一样。
  • 然后我得到它错误:@Provides 方法必须返回一个值(非 void) public final void provideRealmContext(@org.jetbrains.annotations.NotNull()

标签: android kotlin dependency-injection realm dagger-hilt


【解决方案1】:

我认为这可能有效。 Hilt 可以注入应用程序上下文。退房 @ApplicationContext

import android.content.Context
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import io.realm.Realm
import io.realm.RealmConfiguration
import javax.inject.Singleton

@InstallIn(SingletonComponent::class)
@Module
object RealmModule {
    @Provides
    @Singleton
    fun providesRealmDatabase(
        @ApplicationContext context: Context
    ): Realm {
        Realm.init(context)
        val realmConfiguration = RealmConfiguration
            .Builder()
            .name("Tsavo Project")
            .build()
        Realm.setDefaultConfiguration(realmConfiguration)
        return Realm.getDefaultInstance()
    }
}

【讨论】:

    【解决方案2】:
    @Singleton
    @Provides
    fun provideRealm(@ApplicationContext context: Context): Realm =
        try {
            Realm.init(context)
            Realm.getDefaultInstance()
        } catch (e: Exception) {
            Realm.getInstance(provideRealmConfig())
        }
    
    private fun provideRealmConfig(): RealmConfiguration = RealmConfiguration.Builder().build()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-09
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多