Koin:为什么在 myObject.scope.close() 之后,它仍然有效?

Koin: Why after myObject.scope.close(), it still works?

我有一个范围

scope<MyObject> {
    scoped { Presenter() }
}

然后当我得到演示者时,这有效。

val presenter = myObject.scope.get<Presenter>(Presenter::class.java)

同样,当我可以分配一个范围变量时,然后获取演示者。

val myScope = myObject.scope
val presenter = myScope.get<Presenter>(Presenter::class.java)

如果我们关闭它,这仍然可以

val presenter = myObject.scope.get<Presenter>(Presenter::class.java)
myObject.scope.close()
val presenter2 = myObject.scope.get<Presenter>(Presenter::class.java)

但是,如果我分配给另一个作用域变量,然后关闭它,它将失败。

val myScope = myObject.scope
val presenter = myScope.get<Presenter>(Presenter::class.java)
myScope.close()
val presenter2 = myScope.get<Presenter>(Presenter::class.java) // Crash here.

同样,如果我这样做,它也会崩溃

val myScope = myObject.scope
val presenter = myScope.get<Presenter>(Presenter::class.java)
myScope.close()
myScope.getOrCreateScope() // Crash here
val presenter2 = myScope.get<Presenter>(Presenter::class.java)

我明白 close() 之后,示波器无法再提供 presenter。 就是不明白为什么myObject.scope.close()之后,myObject.scope还能提供presenter? (和同一位主持人)

val presenter = myObject.scope.get<Presenter>(Presenter::class.java)
myObject.scope.close()
val presenter2 = myObject.scope.get<Presenter>(Presenter::class.java) 
// presenter1 == presenter2

表面上看,myObject.scope实际上是myObject.getOrCreateScope()。因此,即使在 myObject.scope.close() 被调用之后,myObject.scope 仍然会工作,因为它会再次创建一个新的范围。

但是如果我们执行下面的操作(关闭它),无论使用 myScope 还是 myObject.scope,它仍然会关闭,因此从 myScope 再次使用它会崩溃。

val myScope = myObject.scope
val presenter = myScope.get<Presenter>(Presenter::class.java)
myObject.scope.close() // or myScope.scope
val presenter2 = myScope.get<Presenter>(Presenter::class.java) // this will crash

讨论与发现见下文 https://github.com/InsertKoinIO/koin/issues/786