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

Named qualifier does not work with view models #1500

Closed
koral-- opened this issue Jan 5, 2023 · 6 comments
Closed

Named qualifier does not work with view models #1500

koral-- opened this issue Jan 5, 2023 · 6 comments
Labels
android status:checking currently in analysis - discussion or need more detailed specs type:issue
Milestone

Comments

@koral--
Copy link

koral-- commented Jan 5, 2023

Describe the bug
It seems to be the same as #452

When providing ViewModels using viewModel with named in module and injecting using by viewModel the same instances are injected regardless of names.

To Reproduce
Use the following module:

            modules(module {
                viewModel(named("A")) { SampleViewModel("A") }
                viewModel(named("B")) { SampleViewModel("B") }
            })

And the injection:

    private val vm1 by viewModel<SampleViewModel>(named("A"))
    private val vm2 by viewModel<SampleViewModel>(named("B"))

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.e("koin","vm1: $vm1 ${vm1.label}")
        Log.e("koin","vm2: $vm2 ${vm2.label}")
    }

Logcat output:

vm1: pl.droidsonroids.sample.SampleViewModel@<hash> A
vm2: pl.droidsonroids.sample.SampleViewModel@<hash> A

Expected behavior
Logcat output:

vm1: pl.droidsonroids.sample.SampleViewModel@<hash> A
vm2: pl.droidsonroids.sample.SampleViewModel@<hash> B

Koin project used and used version (please complete the following information):
io.insert-koin:koin-android:3.3.2

Additional moduleDefinition
N/A

Reproducer project: /~https://github.com/DroidsOnRoids/KoinQualifier

Additional log:

I  [init] declare Android Context
D  (+) index 'android.content.Context::_root_' -> '[Singleton:'android.app.Application',binds:android.content.Context,android.app.Application]'
D  (+) index 'android.app.Application::_root_' -> '[Singleton:'android.app.Application',binds:android.content.Context,android.app.Application]'
D  (+) index 'pl.droidsonroids.sample.SampleViewModel:B:_root_' -> '[Factory:'pl.droidsonroids.sample.SampleViewModel',qualifier:B]'
D  (+) index 'pl.droidsonroids.sample.SampleViewModel:A:_root_' -> '[Factory:'pl.droidsonroids.sample.SampleViewModel',qualifier:A]'
I  loaded 4 definitions in 0.186146 ms
D  Eager instances ...
D  Eager instances created in 0.010052 ms
D  |- 'pl.droidsonroids.sample.SampleViewModel' with qualifier 'A' ...
D  | (+) '[Factory:'pl.droidsonroids.sample.SampleViewModel',qualifier:A]'
D  |- 'pl.droidsonroids.sample.SampleViewModel' in 0.587239 ms
@arnaudgiuliani arnaudgiuliani added android type:issue status:checking currently in analysis - discussion or need more detailed specs labels Jan 9, 2023
@arnaudgiuliani arnaudgiuliani added this to the android-3.3.3 milestone Jan 9, 2023
@arnaudgiuliani
Copy link
Member

interesting 🤔

@mlilienberg
Copy link

Named qualifiers also do not work when using constructor DSL like documented here: https://insert-koin.io/docs/reference/koin-core/dsl-update#dsl-options

//Does work
single<Interceptor>(named(Interceptor.PLUTO)) {
    PlutoInterceptor()
} bind Interceptor::class

//Does not work
singleOf<Interceptor>(::PlutoInterceptor) {
    named(Interceptor.PLUTO)
} bind Interceptor::class

Maybe the cause is related maybe not. Let me know if i should create a separate ticket for this.

@arnaudgiuliani
Copy link
Member

//Does not work
singleOf(::PlutoInterceptor) {
named(Interceptor.PLUTO)
} bind Interceptor::class

yes, here there is confusion with 2 functions named. Check if you have org.koin.core.module.dsl.named import

@mlilienberg
Copy link

Thanks @arnaudgiuliani. This was the issue.

@arnaudgiuliani
Copy link
Member

i need to find a solution to avoid confusion with the 2 named() functions. I could reproduce the VM issue also of @koral--

@arnaudgiuliani
Copy link
Member

Will be release in koin-android 3.3.3

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

No branches or pull requests

3 participants