如何在kodein中调用的第二时间重新绑定模块? [英] How to Rebind the Module at secound time called in kodein?

查看:128
本文介绍了如何在kodein中调用的第二时间重新绑定模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个android应用程序,它是由kotlin开发的,我们也使用kodein依赖项来绑定数据.第一次绑定数据时,它将正确绑定,但在第二次调用时不会绑定. /p>

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)
        }
    }

问题是,第一次绑定时 该代码将执行

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

"InitializingCurrentAddressProvider()"此类成功调用并执行.

但是 当我尝试拨打这些行

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

第二次执行该行,但

"InitializingCurrentAddressProvider()"此类不执行.多数民众赞成在问题,如果第二个类是执行手段 我将得到结果,然后自动结果将被绑定.但它不会执行.

解决方案

调用kodein.instance()时,您将获得绑定的一个实例,并且由于绑定类型为单例:您将获得与先前相同的实例,并且只有一次它将不再创建,因此InitializingCurrentAddressProvider(keyStore,instance(),instance(),applicationContext,i)仅被调用一次.每次您获取instance(),InitializingCurrentAddressProvider(keyStore,instance(),instance(),applicationContext,i)时,将单例切换到provider,以查看调用InitializingCurrentAddressProvider(keyStore,instance(),instance(),applicationContext,i). /p>

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.

解决方案

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.

这篇关于如何在kodein中调用的第二时间重新绑定模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆