Scope loses old object instances.
See original GitHub issuehi @arnaudgiuliani thanks for good product, found some issue in scopes.
after creating new id
on same scope
all old instances in that scope are lost
here is clear explanation.
definition of my scope in modules.
scope(named("my_scope")) {
scoped {
MyClass()
}
}
To Reproduce
usage in my Activity
val koin = getKoin()
val scope1 = koin.createScope("A", named("my_scope"))
val scope2 =koin.createScope("B", named("my_scope"))
//get instnace from id A
val instanceA = scope1.get<MyClass>()
//get instnace from id B
val instanceB = scope2.get<MyClass>()
//get instnace from id A
val instanceACopy1 = scope1.get<MyClass>()
//instances must be equals. they both are from id A
println("instanceA === instanceACopy1 ${instanceA === instanceACopy1}")
//output "instanceA === instanceACopy1 true"
this case works correct , output instanceA === instanceACopy1 true
but. lets try this
val koin = getKoin()
val scope1 = koin.createScope("A", named("my_scope"))
val scope2 =koin.createScope("B", named("my_scope"))
//get instnace from id A
val instanceA = scope1.get<MyClass>()
//get instnace from id B
val instanceB = scope2.get<MyClass>()
//get instnace from id A
val instanceACopy1 = scope1.get<MyClass>()
//what happens here, every time when I create new Id in exact scope, all old IDs in that scope are removed.
val scope3 =koin.createScope("C", named("my_scope"))
val instanceACopy2 = koin.getOrCreateScope("A", named("my_scope")).get<MyClass>()
//instances must be equals. they both are from id A
println("instanceA === instanceACopy1 ${instanceA === instanceACopy1}")//output "instanceA === instanceACopy1 true"
//instances must be equals , the both are from id A, but they are not.
println("{instanceA === instanceACopy2 ${instanceA === instanceACopy2}")
//output "instanceA === instanceACopy2 false"
so problem is ,every time when I create new id
in exact scope
all previosly created objects (ScopeDefinitions) in that id
are removed
Expected behavior
I need to receive same object instances from same id
every time befor I close it.
Koin project used and used version (please complete the following information):
org.koin:koin-core:2.0.1
org.koin:koin-android:2.0.1
org.koin:koin-android-scope:2.0.1
org.koin:koin-java:2.0.1
PS Debugged and found the problem every time when I create new id in exact scope this method is called
fun createInstanceHolder() {
this.instance = when (kind) {
Kind.Single -> SingleDefinitionInstance(this)
Kind.Factory -> FactoryDefinitionInstance(this)
Kind.Scoped -> ScopeDefinitionInstance(this)
}
}
ScopeDefinitionInstance
contains values
map, where are stored my old instances.
so every time when you create this.instance = ...
all old instances are removed
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (2 by maintainers)
Hello,
should be fixed in 2.1.0-alpha-8
as I understand @aschattney pull request fixed this issue.