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

旧时模样 提交于 2019-12-24 20:54:41

问题


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:


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.



来源:https://stackoverflow.com/questions/52565281/how-to-rebind-the-module-at-secound-time-called-in-kodein

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!