How to Rebind the Module at secound time called in kodein?

195 views Asked by At

I have an android app, its developed in kotlin, also we use kodein dependence for binding the data.When the binding the data first time it will bind correctly but it does not bind at second time call.

inner class CallmyClass() : MultiDexApplication(), KodeinAware {

        val diModel = Kodein.Module {
            bind<ExchangeRateProvider>() with singleton { CryptoCompareExchangeProvider(this@App, instance()) }
            bind<SyncProgressProvider>() with singleton { SyncProgressProvider() }
            bind<WallethKeyStore>() with singleton { keyStore }
            bind<Settings>() with singleton { KotprefSettings }

            bind<CurrentTokenProvider>() with singleton { CurrentTokenProvider(instance()) }

            bind<AppDatabase>() with singleton { Room.databaseBuilder(applicationContext, AppDatabase::class.java, "maindb").build() }
            bind<NetworkDefinitionProvider>() with singleton { NetworkDefinitionProvider(instance()) }

            bind<CurrentAddressProvider>() with singleton { InitializingCurrentAddressProvider(keyStore, instance(), instance(), applicationContext,1) }

            bind<FourByteDirectory>() with singleton { FourByteDirectoryImpl(instance(), applicationContext) }

        }

        val appDiModule = Kodein.Module(allowSilentOverride = true) {
            import(diModel)
        }

        override val kodein: Kodein = Kodein {
            import(appDiModule)
        }
    }

the problemo is, when binding at first time this code will excute

bind<CurrentAddressProvider>() with singleton { InitializingCurrentAddressProvider(keyStore, instance(), instance(), applicationContext,i) }

and "InitializingCurrentAddressProvider()" this class called and executed successfully.

BUT when i try to call these line

bind<CurrentAddressProvider>() with singleton { InitializingCurrentAddressProvider(keyStore, instance(), instance(), applicationContext,i) }

at second time the line is executed but

"InitializingCurrentAddressProvider()" this class does not execute. thats the problem, if the second the class is execute means i will get the result then automatically result will bind. but it does not execute.

1

There are 1 answers

0
mortezahosseini On

When you call kodein.instance() you get an instance of your binding and because your binding type is singleton: you get same instance as previous and just one time it will create, not any more, so InitializingCurrentAddressProvider(keyStore, instance(), instance(), applicationContext,i) just called once. switch singleton to provider to see calling InitializingCurrentAddressProvider(keyStore, instance(), instance(), applicationContext,i) every time you get instance(), InitializingCurrentAddressProvider(keyStore, instance(), instance(), applicationContext,i) executed.