0

漏洞

当我尝试打开片段然后用相同的片段替换时,我发现了一个问题。在我们的 prod 应用程序中,这是一个流行的案例。

java.lang.IllegalStateException: Definition without any InstanceContext - [type:Scope,scope:'com.abc.view.fragment.BrowseTaskFragment', primary_type:'com.abc.viewModel.BrowseTaskVM']
        at org.koin.core.definition.BeanDefinition.resolveInstance(BeanDefinition.kt:72)
        at org.koin.core.scope.Scope.resolveInstance(Scope.kt:141)
        at org.koin.core.scope.Scope.get(Scope.kt:131)
        at com.abc.view.fragment.BrowseTaskFragment$$special$$inlined$inject$1.invoke(Scope.kt:274)
        at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
        at com.abc.view.fragment.BrowseTaskFragment.getMViewModel(Unknown Source:25)
        at com.abc.view.fragment.BrowseTaskFragment.getMViewModel(BrowseTaskFragment.kt:37)
        at com.abc.base.BaseFragment.performViewModelBinding(BaseFragment.kt:55)
        at com.abc.base.BaseFragment.onViewCreated(BaseFragment.kt:31)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1471)
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802)
        at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
        at androidx.fragment.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:201)
        at android.app.ActivityThread.main(ActivityThread.java:6810)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

重现行为的步骤:

  • 一旦我点击可见的现有片段,我就有了 BottomNavigationView,超越了崩溃。

Koin 版本:2.0.1

模块

val viewModelModule = module {
     scope(named<BrowseTaskFragment>()) {
        scoped { BrowseTaskVM() }
      }
    }

应用类

startKoin {
            androidContext(this@AbcApplication)
            modules(listOf(appModule, stateModule, apiModule, viewModelModule))
        }
4

2 回答 2

4

您可以通过创建范围来做到这一点。首先在您的模块类中通过命名 koin 方法创建范围

val viewModelModule = module {
 scope(named<BrowseTaskFragment>()) {
   scoped { BrowseTaskVM() }
  }
}

在您的片段中排名第二

private val viewModelScope = getKoin().getOrCreateScope("Scope1",named<BrowseTaskFragment>())
private val browseTaskVM: BrowseTaskVM= viewModelScope.get()

最后在 onDestoy 方法中关闭您的范围。

 override fun onDestroy() {
    super.onDestroy()
    viewModelScope.close()
}
于 2019-06-23T06:03:28.553 回答
0

像这样在应用程序类中定义

  startKoin {
           androidContext(this@ApplicationContext)
        // your modules
        modules(listOf(your modules))
    }

并在你的模块中传递上下文

于 2019-06-21T08:20:49.757 回答