【问题标题】:scoped injection with koin 3使用 koin 3 进行范围注入
【发布时间】:2021-08-13 02:22:46
【问题描述】:

我正在尝试将我的 koin 使用从 2.1.6 升级到 3.0.2,但在作用域注入时遇到了问题。

我有 MVP,其中 Activity/Fragment 是视图,我想在演示者中注入视图。

所以我有

module {
    scope(named<MainActivity>()) {
    scoped<View> { getSource() }
    scoped<Presenter> {
         MainPresenter(
             view = get()
         )
    }
}

在 2.1.6 中,我曾经这样做过,一切都很好:

class MainActivity :
    AppCompatActivity(),
    MainContract.View {

    private val presenter: MainContract.Presenter by currentScope.inject()
    ...
}

然后在 MainActivity 我现在有:

class MainActivity :
    AppCompatActivity(),
    MainContract.View,
    AndroidScopeComponent {
    override val scope : Scope by activityScope()
    private val presenter: MainContract.Presenter by scope.inject()

...
}

和演示者:

   class MainPresenter(
       private val view: MainContract.View
   ){
       ...
   }

但它无法获取源对象,我得到了错误:

Instance creation error : could not create instance for [Single:'uk.co.sentinelweb.cuer.app.ui.main.MainContract$View',scope:q:'uk.co.sentinelweb.cuer.app.ui.main.MainActivity']: java.lang.IllegalStateException: Can't use Scope source for uk.co.sentinelweb.cuer.app.ui.main.MainContract$View - source is:null

(即,当它尝试创建演示者时,它找不到范围内的 MainActivity)

这是现有代码(使用 2.1.6) https://github.com/sentinelweb/cuer/blob/develop/app/src/main/java/uk/co/sentinelweb/cuer/app/ui/main/MainActivity.kt

我还有更多的重写工作要做吗?我正在努力在 koin 文档中找到一个范围注入的好例子,而且很多看起来都很旧。很多项目似乎没有使用范围。

所以,如果有人能告诉我这里出了什么问题,或者给我指出一个类似 id 的好例子,不胜感激!

【问题讨论】:

    标签: android mvp koin koin-scope


    【解决方案1】:

    因此,对于生命周期感知扩展方法来说,它似乎只是没有设置范围变量 - 也许他们对内存泄漏感到偏执,但由于范围已被清除并且在销毁生命周期方法上 - 这真的不应该是一个问题。

    我的解决方案是创建实际上只是传入源代码的新扩展方法 - 我不确定为什么会出现问题。这里有一个问题https://github.com/InsertKoinIO/koin/issues/851

    package xxx
    
    import android.app.Service
    import androidx.activity.ComponentActivity
    import androidx.fragment.app.Fragment
    import androidx.lifecycle.LifecycleOwner
    import org.koin.android.scope.AndroidScopeComponent
    import org.koin.android.scope.createScope
    import org.koin.android.scope.getScopeOrNull
    import org.koin.androidx.scope.LifecycleScopeDelegate
    import org.koin.core.Koin
    import org.koin.core.component.getScopeId
    import org.koin.core.component.getScopeName
    import org.koin.core.context.GlobalContext
    import org.koin.core.context.KoinContext
    import org.koin.core.scope.Scope
    import kotlin.properties.ReadOnlyProperty
    import kotlin.reflect.KProperty
    
    /** copied from org.koin.androidx.scope.FragmentExt but scope wont link as fragment is not attached */
    fun ComponentActivity.activityScopeWithSource() = LifecycleScopeWithSourceDelegate(this)
    
    /** copied from org.koin.androidx.scope.FragmentExt but scope wont link as fragment is not attached */
    fun Fragment.fragmentScopeWithSource() = LifecycleScopeDelegate(this) { koin: Koin ->
        koin.createScope(getScopeId(), getScopeName(), this)
    }
    
    /** links the fragment scope to the activity scope */
    fun Fragment.linkScopeToActivity() {
        (this as AndroidScopeComponent).scope.linkTo((requireActivity() as AndroidScopeComponent).scope)
    }
    
    /** copied from org.koin.android.scope.ServiceExtKt  */
    fun Service.serviceScopeWithSource() = lazy { getScopeOrNull() ?: createScope(this) }
    
    /** wraps org.koin.androidx.scope.LifecycleScopeDelegate - to add source  */
    class LifecycleScopeWithSourceDelegate(
        val lifecycleOwner: LifecycleOwner,
        koinContext: KoinContext = GlobalContext,
        createScope: (Koin) -> Scope = { koin: Koin ->
            koin.createScope(
                lifecycleOwner.getScopeId(),
                lifecycleOwner.getScopeName(),
                lifecycleOwner
            )
        },
    ) : ReadOnlyProperty<LifecycleOwner, Scope> {
    
        private val _lifecycleDelegate = LifecycleScopeDelegate(lifecycleOwner, koinContext, createScope)
    
        override fun getValue(thisRef: LifecycleOwner, property: KProperty<*>): Scope {
            return _lifecycleDelegate.getValue(thisRef, property)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多