question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Scope loses old object instances.

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
arnaudgiulianicommented, Dec 17, 2019

Hello,

should be fixed in 2.1.0-alpha-8

1reaction
haykmelqonyancommented, Oct 4, 2019

as I understand @aschattney pull request fixed this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

how much bump will cause a rifle to lose zero
It all just depends. I dropped an old Redfield real hard once, something came loose inside, sent it back, they sent a new...
Read more >
c++ - When is an object "out of scope"?
First, remember that objects in C++ can be created either on the stack or on on the heap. A stack frame (or scope)...
Read more >
CA2000: Dispose objects before losing scope
Cause. A local object of an IDisposable type is created, but the object is not disposed before all references to the object are...
Read more >
What can cause a rifle scope to fail to hold a zero after ...
The most common reason for a modern well manufactured scope (versus a cheap crap scope) to lose zero is in the mounting NOT...
Read more >
Why a Rifle Loses Zero and How to Keep it Zeroed
Common Mistakes That Can Cause a Rifle Scope to Lose Its Zero. It is crucial to ensure that the objective bell does not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found