简体   繁体   English

Kotlin和Room的Dagger 2错误

[英]Dagger 2 Error with Kotlin and Room

I been refactoring an app to Kotlin and currently I have been facing a weird error from Dagger. 我一直在将应用重构为Kotlin,目前我正面临Dagger的奇怪错误。 Im trying to implement a MVVM design but im hard stuck with the dagger error. 我试图实现MVVM设计,但我一直坚持使用匕首错误。

AppModule 应用模块

@Module
class AppModule(val app: App) {

companion object {
    private var INSTANCE: RecorderisDB? = null

    private fun getInstance(context: Context): RecorderisDB?{
        if (INSTANCE == null) {
            synchronized(RecorderisDB::class){
                INSTANCE = Room.databaseBuilder(context.applicationContext,
                        RecorderisDB::class.java,
                        "recorderis.db")
                        .build()
            }
        }
        return INSTANCE
    }

    fun destroyInstance(){
        INSTANCE = null
    }
}

@Provides @Singleton
fun provideApp() = app

@Provides @Singleton @Nullable
fun getDB(context: Context): RecorderisDB? = getInstance(context)

@Provides @Singleton
fun provideDateVM(db: RecorderisDB): DateViewModel {
    return DateViewModel(db)
}

AppComponent AppComponent

@Singleton
@Component(modules = [(AppModule::class)])
interface AppComponent {

fun inject(app: App)

fun inject(form: Form)
}

DateViewModel DateViewModel

class DateViewModel @Inject constructor(val dB: RecorderisDB){

fun createDate(name: String, symbol: String, date: String): Completable {
    return Completable.fromAction{ dB.getDateDao().newDate(Date(name, symbol, date))}
}

Form.kt 表格

class Form : AppCompatActivity() {

@Inject
lateinit var dateVM: DateViewModel
public override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_form)
    App.graph.inject(this)

    initDialog()
    setUpRecyclerView()
 }

Stacktrace Log 堆栈跟踪日志

English is not my first language but this error i think is being contradictory? 英语不是我的母语,但是我认为这个错误是矛盾的? Is telling me that my DB is not nullable BUT is being provided? 告诉我我的数据库不可为空,但正在提供BUT吗? Basically what i Have i my companion object inside the AppModule. 基本上,我在AppModule中拥有我的同伴对象。

15:27:21.882 [ERROR] [org.gradle.api.Task] e: C:\Users\diego\Apps\Recorderis\app\build\tmp\kapt3\stubs\debug\tech\destinum\recorderis\DI\AppComponent.java:13: 
error: [Dagger/Nullable] tech.destinum.recorderis.Data.RecorderisDB is not nullable, 
but is being provided by @org.jetbrains.annotations.Nullable @Singleton 
@Provides tech.destinum.recorderis.Data.RecorderisDB 
tech.destinum.recorderis.DI.AppModule.getDB(android.content.Context)
public abstract void inject(@org.jetbrains.annotations.NotNull()
                     ^
  tech.destinum.recorderis.Data.RecorderisDB is injected at
      tech.destinum.recorderis.DI.AppModule.provideDateVM(db)
  tech.destinum.recorderis.Data.ViewModels.DateViewModel is injected at
      tech.destinum.recorderis.activities.Form.dateVM
  tech.destinum.recorderis.activities.Form is injected at
      tech.destinum.recorderis.DI.AppComponent.inject(tech.destinum.recorderis.activities.Form)

Well it specifically says that the problem is that you are injecting RecorderisDB , even though you are providing RecorderisDB? 那么它具体说,问题是,你是注入RecorderisDB ,即使您提供RecorderisDB? .

The solution? 解决方案? Dagger already handles the double-checked locking for you just by using @Singleton @Provides . Dagger已经通过使用@Singleton @Provides为您处理了双重检查锁定。 There is no need for that code at all. 完全不需要该代码。

@Module
class AppModule(val app: App) {

    @Provides
    fun provideApp() = app

    @Provides @Singleton
    fun getDB(context: App): RecorderisDB = Room.databaseBuilder(context.applicationContext,
                        RecorderisDB::class.java,
                        "recorderis.db")
                        .build()

    @Provides 
    // @Singleton // are you CERTAIN this is singleton?
    fun provideDateVM(db: RecorderisDB): DateViewModel {
        return DateViewModel(db)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM