InsertKoinIO / koin

Koin - a pragmatic lightweight dependency injection framework for Kotlin & Kotlin Multiplatform
https://insert-koin.io
Apache License 2.0
8.77k stars 695 forks source link

Compose view model constantly returning new instances with non-root scope #1832

Closed thadcodes closed 3 months ago

thadcodes commented 3 months ago

Describe the bug If using compose you fetch two different view model classes using org.koin.androidx.compose.koinViewModel with a non-root scope, every time a recompose happens a new instance of each view model will be returned.

Note: I know the issue is with the way the key is being generated and will submit a PR shortly as well

To Reproduce Steps to reproduce the behavior:

  1. Run Sample application
  2. Read the logcat entries after a moment of displaying

Expected behavior Every time `view model: {Instance}" is printed it should be the same time

Koin module and version: koin-androidx-compose:3.5.3 koin-android:3.5.3

Snippet or Sample project to help reproduce Sample project to reproduce the issue. It's a reused project from reporting another issue so disregard the name koinQualifierIssue.zip

wax911 commented 3 months ago

@thadcodes out of curiosity, might this also be related to https://github.com/InsertKoinIO/koin/issues/1831 which sounds similar 🤔

thadcodes commented 3 months ago

@thadcodes out of curiosity, might this also be related to #1831 which sounds similar 🤔

Yes that could be the same, but part of what causes this is having 2 view model classes used in the same scope and same view lifecycle. I suspect a way to test is to give your view model a qualifier even if you don't need it and see if the issue goes away

arnaudgiuliani commented 3 months ago

Yes, known issue in 3.5.3 due to VM key generation. It's fixed in 3.5.4.

thadcodes commented 3 months ago

Yes, known issue in 3.5.3 due to VM key generation. It's fixed in 3.5.4.

Can you point to the fix? I'm unclear how this wouldn't still be happening on the main branch

thadcodes commented 3 months ago

Yes, known issue in 3.5.3 due to VM key generation. It's fixed in 3.5.4.

Thanks, I see that scope was removed from the key in 3.5.4, so I'll close