Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed Scope does not influence resolveViewModel #1654

Closed
peterdk opened this issue Sep 14, 2023 · 3 comments
Closed

Closed Scope does not influence resolveViewModel #1654

peterdk opened this issue Sep 14, 2023 · 3 comments
Labels
scope status:checking currently in analysis - discussion or need more detailed specs viewmodel
Milestone

Comments

@peterdk
Copy link

peterdk commented Sep 14, 2023

Describe the bug
When using a scope to resolve a viewmodel, then close the scope, create a new one and resolve viewmodel again, it will return the viewmodel that is created in the first scope, not a new one. Which I would expect?

To Reproduce

//class A
 val fooScope = GlobalContext.get().getOrCreateScope("foo", named("foo"))
//Class B
 val fooScope = GlobalContext.get().getOrCreateScope("foo", named("foo"))
        val viewModel = resolveViewModel(
            vmClass = T::class,
            viewModelStore = this.viewModelStore, //same viewmodel store in all calls
            extras = extrasProducer?.invoke() ?: MutableCreationExtras(),
            parameters = parameters,
            scope = widgetsScope,
            qualifier = qualifier,
        )

//Class A
fooScope.close()

//Class B creates scope and resolves viewmodel again

I would expect that a new scope would result in a fresh viewmodel, not in the existing one.
However, using System.identityhash shows that the scope is indeed renewed, but the resolved viewmodel is the same.

I am not sure if this is actually supported.

Koin module and version:
koin-3.5.0 (but also 3.4 and earlier)

@arnaudgiuliani arnaudgiuliani added status:checking currently in analysis - discussion or need more detailed specs viewmodel scope labels Sep 18, 2023
@arnaudgiuliani arnaudgiuliani added this to the android-3.5.1 milestone Sep 18, 2023
@arnaudgiuliani
Copy link
Member

seems easy to reproduce 👍

@arnaudgiuliani
Copy link
Member

the thing is, it will depend on the viewModelStore. ViewModels are stored in this. This would explain why you find the same again. Perhaps, a scope key may be needed for this case.

@arnaudgiuliani
Copy link
Member

Can you test that on 3.5.2-RC1? I'm preparing patch for that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope status:checking currently in analysis - discussion or need more detailed specs viewmodel
Projects
None yet
Development

No branches or pull requests

2 participants