Koin: Injecting into Worker class from WorkManager

Created on 5 Jul 2018  路  6Comments  路  Source: InsertKoinIO/koin

We need to be able to inject classes into the new Worker class from WorkManager API's. This will be useful if we need to inject NetworkRepo's or LocalDbRepo's etc..

Target Koin project
https://github.com/sriramr98/Wally

android question

Most helpful comment

You can make your Worker class implement KoinComponent interface
then simply in doWork() you can call

    override fun doWork(): Result {
        val networkRepo: NetworkRepo by inject()
        ...
    }

All 6 comments

Hi @sriramr98 , I'll check that 馃憤

You can access applicationContext from Worker, isn't it enough?

@sockeqwe How do you inject a class using the applicationContext ?

You can make your Worker class implement KoinComponent interface
then simply in doWork() you can call

    override fun doWork(): Result {
        val networkRepo: NetworkRepo by inject()
        ...
    }

Thanks, @stasbar. Works perfectly

You can make your Worker class implement KoinComponent interface
then simply in doWork() you can call

    override fun doWork(): Result {
        val networkRepo: NetworkRepo by inject()
        ...
    }

nice works perfectly as well!!!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

miladsalimiiii picture miladsalimiiii  路  3Comments

hkelidari picture hkelidari  路  3Comments

luna-vulpo picture luna-vulpo  路  4Comments

LukasAnda picture LukasAnda  路  3Comments

guymclean picture guymclean  路  3Comments