Hey <@U2JKKPMEE> and <@UHM5QMQ81>, me and <@U5WBBT...
# koin
r
Hey @arnaud.giuliani and @Marko Mitic, me and @ritesh work on the same project. Here is our use case We are working on a library project that will be used by other application. One of the application needs to have two instances of the our SDK based on the configuration. Both the instance need to be in memory at that same time. We have an initialization class that creates the KoinApplication instance and loads the module. Consider that we have an activity and using koin we inject a viewModel. Now the ViewModel is based on the configuration Object passed by the consuming application. So We need different view model based on each configuration Object With 2.X Koin we have Activity implementing the KoinComponent but how should we inform Activity which KoinApplication to use so that it returns the correct koin instance
m
I guess depends on your configuration. Considering that you have two instances of KoinApplication/SDK, each activity using your SDK will need to pick which instance it needs and use it to get Koin instance it needs for KoinComponent
So, instance of your SDKs API will need to provide Koin instance to app
Maybe like this:
Copy code
class MyActivity: Activity(), KoinComponent {
    override fun getKoin(): Koin = sdk.koin //sdk is one of your instances
r
So our Initialization class has KoinApplication instance and we did exactly the MyActivity code you have shared but it pick up the second configuration passed thereby overriding the first one
👌 1
r
@Marko Mitic Yeah. So what we are doing is, based on our config, when the sdk is initialiazed, we create two koin instace, say
instance1
and
instance2
. And we do override, getKoin(), we have a check there, if current sdk is using whicn config, and based on that we give the koin instance. Problem is it always overrides the second instance.
r
Copy code
class ABC {

 internal lateinit var localInstance: KoinApplication

fun init(
configuration : Configuration 
): SDKInstance {
  val modules: List<Module> = koinModules + module(configuration)
  localInstance = koinApplication {
    androidContext(context)
    modules(modules)
  }
And our activity code is exactly what you have shared
But when we fetch the configuration on Instance1 of the SDK it gives us that of Instance2
m
From what I can see, it looks good
r
Is it possible to switch between instances at runtime and get dependecies from di graph
1
or
2
@Marko Mitic
m
Maybe @arnaud.giuliani can help. Could you show your configuration module?
yeah, from what I know, that should work without issues
r
ok
Sending you the config in few minutes.
r
Copy code
module {
      single {
        Configuration(name)
      }
    }
@Marko Mitic configuration module looks like this
m
I don't see the issue there. Could you try adding a method to your API that returns the needed viewmodel instead of using koin&koinComponent?
r
Any initial thoughts of what might be a problem?
m
it's hard to say
r
Copy code
class MyActivity: Activity(), KoinComponent {
    override fun getKoin(): Koin = sdk.koin //sdk is one of your instances
@Marko Mitic What can be an optimum way to pass the sdk instance to an activity?
r
Here's the base activity.
Now, if from consuming app, I Do
Copy code
val sdk1 = CustomSdk.initDI(firstConfig)
val sdk2 = CustomSdk.initDI(secondConfig)
It will create two koin instances. Now, based on flag, it should take from the respective cotainer. But, it
sdk2
overrides
sdk1
. If activity is started from
sdk1
@Marko Mitic @arnaud.giuliani
r
@arnaud.giuliani Any thoughts over the use case will be really helpful