简体   繁体   English

在多模块应用程序中使用 koin

[英]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 生命周期和你想要做什么,你必须在onStopunload

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.

相关问题 在多模块 Android 应用程序中导航回来 - Navigating back in a multi-module Android application 使用 Dagger2 的多模块 android 应用程序,无需在功能模块中使用 `kapt` - Multi-module android application with Dagger2 without using `kapt` in the feature module 在多模块项目中使用 Firebase - Using Firebase in a multi-module project 使用 gradle DSL 在多模块项目中声明风味依赖项 - Declaring flavor dependencies in multi-module project using gradle DSL 在多功能模块应用程序中,我在哪里加载和卸载 Koin 模块并保持可测试性 - Where do I load and unload Koin Modules, in a multi-feature module application and keep it testable 模块如何使用Gradle多模块使用另一个模块的资源 - How module can use resources of another module using gradle multi-module 如何使用 Koin DI 将应用程序上下文从“app”模块注入到“.network”模块 - How to Inject application context from 'app' module to 'network' module using Koin DI 使用 Jacoco 测量多模块和动态功能模块 Android 项目中的测试覆盖率 - Measuring tests coverage in multi-module and dynamic feature module Android project using Jacoco Android Studio中的多模块注释处理 - Multi-module annotation processing in Android Studio 具有多模块android库的Gradle配置 - Gradle configuration with multi-module android library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM