hi @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
something I don't understand here: val instanceACopy2 = koin.getOrCreateScope("1", named("my_scope")).get<GracePeriodPageViewModel>()
instanceACopy2 is creaed from instance "1", not "A". Then, normal to be different?! 馃
(you previously created scope instances "A","B" from scope "my_scope")
@arnaudgiuliani sorry its typo.
both instanceACopy2 and instanceA are from id A ,
I have updated.
if you check this part
PS Debugged and found the problem
you will understand issue better .
thanks for answer.
as I understand @aschattney pull request fixed this issue.
Hello.
Does version '2.1.0-alpha-01' contain fix for this bug?
Hello,
should be fixed in 2.1.0-alpha-8
Hello,
should be fixed in 2.1.0-alpha-8
Thanks.
Most helpful comment
Hello,
should be fixed in 2.1.0-alpha-8