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

activity.getViewModel(named("XXX")) is returning wrong viewmodel after Koin 3.1.5 #1364

Closed
SeanBlahovici opened this issue Jun 19, 2022 · 6 comments
Assignees
Labels
android:viewmodel android regression 💥 status:checking currently in analysis - discussion or need more detailed specs type:issue
Milestone

Comments

@SeanBlahovici
Copy link

SeanBlahovici commented Jun 19, 2022

So I tried updating to 3.2.0 from Koin 2.x and I found an obscure bug. I reverted to lower versions until the problem was no longer present, which is version 3.1.5. Meaning the bug was introduced in 3.1.6.

I have two subtypes of an abstract viewModel that are scoped to my MainActivity. I first load the first type using a named qualifier (using the extension function activity.getViewModel() - This is from a non activity or fragment class. But after this, I try getting an instance of the other subtype using a differently named qualifier, but end up receiving the previously loaded type. This only happens after 3.1.5.

To Reproduce

  1. Define two implementations of an abstract viewModel in a koin module. Example:
    viewModel<AbstractViewModel>(named("First type")) { ConcreteViewModel1(get(), get(), get()) } viewModel<AbstractViewModel>(named("Second type")) { ConcreteViewModel2(get(), get(), get()) }

  2. Pass an activity instance to platform agnostic class.

  3. On that instance, call the extension function getViewModel(named("First type")). It should return the correct type (ConcreteViewModel1)

  4. After that, try calling activity.getViewModel(named("Second type")). Notice the type returned is the wrong type, still ConcreteViewModel1

Koin project used and used version (please complete the following information):
koin-android 3.1.6 & koin-android 3.2.0

@ronaldkieft
Copy link

ronaldkieft commented Jul 29, 2022

I experience the same problem when using 3.1.6. I do however only experience the problem when retrieving named view models that are scoped to a navigation graph. When retrieving view models in the regular way (using the default ViewModelOwner) the named qualifier works still as expected in 3.1.6.

@arnaudgiuliani arnaudgiuliani self-assigned this Aug 26, 2022
@arnaudgiuliani arnaudgiuliani added this to the 3.2.1 milestone Aug 26, 2022
@arnaudgiuliani arnaudgiuliani added regression type:issue status:checking currently in analysis - discussion or need more detailed specs and removed regression labels Aug 26, 2022
@arnaudgiuliani arnaudgiuliani modified the milestones: 3.2.1, 3.3.0 Sep 7, 2022
@arnaudgiuliani
Copy link
Member

Google ViewModel API doesn't allow as we use it right now doesn't allow to use qualifier as we were using it.

Koin 3.3.0 bring a great rework on ViewModel API, let's fix it on this version then

@arnaudgiuliani
Copy link
Member

ViewModel API has been fixed is available in koin-android 3.3.0 👍
#1459

@Wirling
Copy link

Wirling commented Nov 3, 2022

@arnaudgiuliani this problem still doesn't seem to be fixed in version 3.3.0. I can still reproduce it as described by @SeanBlahovici. I also encounter this problem with koinNavGraphViewModel.

@Hungon
Copy link

Hungon commented Nov 15, 2022

I've also encountered the same problem even in v3.3.0.

@irgaly
Copy link

irgaly commented Dec 13, 2022

@arnaudgiuliani @SeanBlahovici
Is this problem fixed? If it is not fixed, please reopen this issue for tracking this problem.

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

No branches or pull requests

6 participants