I stumbled across a problem where I have to reinitialize my Retrofit API class because the URL changes, so I want to tell Koin to reinitialize the single dependency. I don't want to use factory because it is still a singleton most of the time.
Stopping and starting koin would be an idea but that is a very heavy and time intense prozess so I don't want to do that.
any ideas?
The module function as the name suggests is used to create a module. single is used to create a singleton instance. This means that Koin will return the same instance of the class when the dependency is needed. We have made Course a singleton since we assume that all the students do the same course.
Koin is a smart Kotlin dependency injection library to keep you focused on your app, not on your tools. Koin gives you simple tools and API to let you build, assemble Kotlin related technologies into your application and let you scale your business with easyness. Define Modules. Start Koin. Start on Android.
Understanding Terminologies in Koin single - it creates a singleton that can be used across the app as a singular instance. factory - it provides a bean definition, which will create a new instance each time it is injected. get() - it is used in the constructor of a class to provide the required dependency.
Koin creates a root context object that holds the configuration for instantiating dependencies and specific parameters of these dependencies. In the case of singleton objects, it also holds references to instances of these dependencies. The dependencies can be described in modules.
After doing A LOT OF RESEARCH, I found this post on GitHub scoping is the solution for my problem.
--- OR ---
using
unloadKoinModules(networkModule)
loadKoinModules(networkModule)
and then get the instance again:
val api: InstanceApi = getKoin().get()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With