[英]Using koin in Multi-module application
Hi in a multi modules app, I am loading child modules using loadKoinModules() and unloading it using unloadKoinModules() in feature module my code looks like嗨,在一个多模块应用程序中,我正在使用 loadKoinModules() 加载子模块并在功能模块中使用 unloadKoinModules() 卸载它我的代码看起来像
class FeatureActivity:AppCompatActivity(){
private val loadFeatures by lazy { loadKoinModules(featureModule) }
private fun injectFeatures() = loadFeatures
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
injectFeatures()
}
override fun onDestroy() {
super.onDestroy()
unloadKoinModules(featureModule)
}
}
Everything works fine but problem start when another instance on same activity is loaded.一切正常,但是当加载相同活动的另一个实例时问题开始了。 While current activity is in background.
当前活动在后台时。 App crash due to error below
由于以下错误导致应用程序崩溃
org.koin.error.BeanOverrideException: Try to override definition with Factory
Is there a way to avoid this error有没有办法避免这个错误
Koin won't let to you redefine an already existing definition (type,name,path …) . Koin 不会让您重新定义已经存在的定义(type,name,path ...) 。 You will run into an error.
你会遇到一个错误。
You need to allow definition override :-您需要允许定义覆盖:-
val featureModule = module {
// override for this definition
single<yourType>(override=true) { YourClass() }
}
ALSO you can override on module level instead of overriding on definition level only:-您还可以在模块级别覆盖而不是仅在定义级别覆盖:-
val featureModule = module(override=true) {
single<yourType> { YourClass() }
}
Important :- Order matters when listing modules and overriding definitions.重要:- 列出模块和覆盖定义时的顺序很重要。 You must have your overriding definitions in last of your module list.
你必须在你的模块列表的最后有你的覆盖定义。
It is somehow correct what you are doing, you can unload dynamically as you do this is why unloadKoinModules
has been added link你在做什么是正确的,你可以动态卸载,这就是为什么添加了
unloadKoinModules
链接
but why aren't you unloading onStop
?但你为什么不卸载
onStop
? according to android lifecycle and what you want to do, you have to unload
in onStop
根据 android 生命周期和你想要做什么,你必须在
onStop
中unload
When activity gets focus onCreate
will occur (and you will load modules), later when activity loses focus, onStop
will occurs (and you will unload modules) and the circle between the events...当活动获得焦点时
onCreate
将发生(并且您将加载模块),稍后当活动失去焦点时,将发生onStop
(并且您将卸载模块)并且事件之间的循环......
class FeatureActivity:AppCompatActivity(){
private val loadFeatures by lazy { loadKoinModules(featureModule) }
private fun injectFeatures() = loadFeatures
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
injectFeatures()
}
override fun onStop() {
super.onStop()
unloadKoinModules(featureModule)
}
}
Some possibilities:一些可能性:
Load your feature module in the top-level application level and don't scope it to any activity lifecycle.在顶级应用程序级别加载您的功能模块,并且不要将其 scope 到任何活动生命周期。
Add a reference-counting wrapper around your module load/unload so the module is not reloaded if it is already loaded, and it is only unloaded when the usage count is zero.在你的模块加载/卸载周围添加一个引用计数包装器,这样如果模块已经加载,它就不会重新加载,并且只有在使用计数为零时才会卸载。 (You can simplify this by not caring about unloading and change the count to just a "initialised" boolean.)
(您可以通过不关心卸载并将计数更改为“初始化”boolean 来简化此操作。)
You should probably unload your modules before calling super.onDestroy()您可能应该在调用 super.onDestroy() 之前卸载您的模块
''' override fun onDestroy() { unloadKoinModules(featureModule) super.onDestroy() } ''' ''' 覆盖乐趣 onDestroy() { unloadKoinModules(featureModule) super.onDestroy() } '''
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.