Koin 的生命周期范围与 activity.scope。它们相同吗?
Koin's lifecycleScope vs activity.scope. Are they the same?
我正在从 https://github.com/InsertKoinIO/koin/blob/master/koin-projects/docs/reference/koin-android/scope.md
学习 Koin 的范围
如果我有如下的Koin模块
val myModule =
module {
scope<MyActivity> { scoped { Presenter() } }
}
在我的 activity 中,我可以做到这一点
class MyActivity : AppCompatActivity() {
private val presenter by lazy {
lifecycleScope.get<Presenter>(Presenter::class.java)
}
// ...
}
或者我可以使用 this.scope
,其中 this
是 MyActivity
对象。
class MyActivity : AppCompatActivity() {
private val presenter by lazy {
this.scope.get<Presenter>(Presenter::class.java)
}
// ...
}
我测试了他们是一样的。两者是相同的,还是不同的?如果不同,它们的区别是什么?
根据我跟踪的代码,lifecycleScope
将在 ON_DESTROY
时自动关闭
所以我从 lifecycleScope
-> getOrCreateAndroidScope()
-> createAndBindAndroidScope
-> bindScope(scope)
-> lifecycle.addObserver(ScopeObserver(event, this, scope))
追踪
代码如下所示。
val LifecycleOwner.lifecycleScope: Scope
get() = getOrCreateAndroidScope()
private fun LifecycleOwner.getOrCreateAndroidScope(): Scope {
val scopeId = getScopeId()
return getKoin().getScopeOrNull(scopeId) ?: createAndBindAndroidScope(scopeId, getScopeName())
}
private fun LifecycleOwner.createAndBindAndroidScope(scopeId: String, qualifier: Qualifier): Scope {
val scope = getKoin().createScope(scopeId, qualifier, this)
bindScope(scope)
return scope
}
fun LifecycleOwner.bindScope(scope: Scope, event: Lifecycle.Event = Lifecycle.Event.ON_DESTROY) {
lifecycle.addObserver(ScopeObserver(event, this, scope))
}
class ScopeObserver(val event: Lifecycle.Event, val target: Any, val scope: Scope) :
LifecycleObserver, KoinComponent {
/**
* Handle ON_STOP to release Koin modules
*/
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onStop() {
if (event == Lifecycle.Event.ON_STOP) {
scope.close()
}
}
/**
* Handle ON_DESTROY to release Koin modules
*/
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onDestroy() {
if (event == Lifecycle.Event.ON_DESTROY) {
scope.close()
}
}
}
我正在从 https://github.com/InsertKoinIO/koin/blob/master/koin-projects/docs/reference/koin-android/scope.md
学习 Koin 的范围如果我有如下的Koin模块
val myModule =
module {
scope<MyActivity> { scoped { Presenter() } }
}
在我的 activity 中,我可以做到这一点
class MyActivity : AppCompatActivity() {
private val presenter by lazy {
lifecycleScope.get<Presenter>(Presenter::class.java)
}
// ...
}
或者我可以使用 this.scope
,其中 this
是 MyActivity
对象。
class MyActivity : AppCompatActivity() {
private val presenter by lazy {
this.scope.get<Presenter>(Presenter::class.java)
}
// ...
}
我测试了他们是一样的。两者是相同的,还是不同的?如果不同,它们的区别是什么?
根据我跟踪的代码,lifecycleScope
将在 ON_DESTROY
所以我从 lifecycleScope
-> getOrCreateAndroidScope()
-> createAndBindAndroidScope
-> bindScope(scope)
-> lifecycle.addObserver(ScopeObserver(event, this, scope))
代码如下所示。
val LifecycleOwner.lifecycleScope: Scope
get() = getOrCreateAndroidScope()
private fun LifecycleOwner.getOrCreateAndroidScope(): Scope {
val scopeId = getScopeId()
return getKoin().getScopeOrNull(scopeId) ?: createAndBindAndroidScope(scopeId, getScopeName())
}
private fun LifecycleOwner.createAndBindAndroidScope(scopeId: String, qualifier: Qualifier): Scope {
val scope = getKoin().createScope(scopeId, qualifier, this)
bindScope(scope)
return scope
}
fun LifecycleOwner.bindScope(scope: Scope, event: Lifecycle.Event = Lifecycle.Event.ON_DESTROY) {
lifecycle.addObserver(ScopeObserver(event, this, scope))
}
class ScopeObserver(val event: Lifecycle.Event, val target: Any, val scope: Scope) :
LifecycleObserver, KoinComponent {
/**
* Handle ON_STOP to release Koin modules
*/
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onStop() {
if (event == Lifecycle.Event.ON_STOP) {
scope.close()
}
}
/**
* Handle ON_DESTROY to release Koin modules
*/
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onDestroy() {
if (event == Lifecycle.Event.ON_DESTROY) {
scope.close()
}
}
}