Tipok
Tipok

Reputation: 695

Provide methods in Dagger 2

I have problem, i don't know why but my variable don't inject and equals null. I provide method like this:

@Provides
@Singleton
fun provideSource(context: Context): DatabaseSource = DatabaseSource(context, Models.DEFAULT, DB_NAME, DB_VERSION)

@Provides
@Singleton
fun provideStoreDB(databaseSource: DatabaseSource): KotlinReactiveEntityStore<Store> = KotlinReactiveEntityStore(KotlinEntityDataStore(databaseSource.configuration))

@Provides
@Singleton
@NotNull
fun provideStores() : Database<Store> = PersistentDatabase(Store::class)

And class for model Store:

it is heir from class "persistable".

@Entity
data class Store(val id: Long = 0, val name: String = "", val description: String = "", val date: String = "") : DisplayableItem, Persistable

Class for works with DB

@Singleton
class PersistentDatabase <T: Any> (val type: KClass<T>) : Database <T> {
    @Inject
    internal lateinit var store: KotlinReactiveEntityStore<T>

    override fun addResult(item: T): Completable = store.insert(item).toCompletable()

    override fun addListResult(item: List<T>): Completable = store.insert(item).toCompletable()

    override fun deleteResult(item: T): Completable = store.delete(item)

    override fun getAllResults(): Single<List<T>> = (store select type).get().observable().toList()

    override fun removeAllResults(): Completable = store.delete(type).get().single().toCompletable()
}

In my repository layer i use it like this:

@Inject
lateinit var database: Database<DetectedLanguageText>

fun getHistory(): Single<List<DetectedLanguageText>> {
    return database.getAllResults()
            .map { it.sortedByDescending { it.date } }
            .transitSuccessToEmitter(historyEmitter)
}

But when i am in function getAllResults the variable

    @Inject
    internal lateinit var store: KotlinReactiveEntityStore<T>

Doesn't inject and equals null. Why it doesn't work for me?

Upvotes: 0

Views: 107

Answers (1)

Guliash
Guliash

Reputation: 163

I can't see where you inject KotlinReactiveEntityStore in PersistentDatabase. You just create an instance in the provides method, but no injection is done.

Upvotes: 1

Related Questions