Koin: How to get the same Instance accoding to a given key

1.7k views Asked by At

I used ViewModelProvider(this).get(myDataIdentifier, MyViewModel::class.java) to get the same viewmodel for each identifier.

Now I want to use Koin for dependency injection but I can't figure out how to get this working.

I can inject data via val viewModel by viewModel() but where am I able to make sure to get the same instance, identified by myDataIdentifier? I can't wrap my head around qualifier, parameter,....

Sorry, maybe this is a dumb question and i just overlooked something.

1

There are 1 answers

2
JakeB On

Try with named components, name your viewmodel (this may now need to be a singleton?)

val myModule = module {
    viewModel(named("myViewModel")) { MyViewModel() }
}

...

val viewModel: MyViewModel by viewModel(named("myViewModel"))

https://engineering.bigshyft.com/koin-2-0-for-android/