Ispam
Ispam

Reputation: 615

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. Im trying to implement a MVVM design but im hard stuck with the dagger error.

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

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

fun inject(app: App)

fun inject(form: Form)
}

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? Basically what i Have i my companion object inside the 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)

Upvotes: 2

Views: 1337

Answers (1)

EpicPandaForce
EpicPandaForce

Reputation: 81539

Well it specifically says that the problem is that you are injecting RecorderisDB, even though you are providing RecorderisDB?.

The solution? Dagger already handles the double-checked locking for you just by using @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)
    }
}

Upvotes: 3

Related Questions