Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android/Koin - How to tell koin that a single dependency should be reinitialized

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?

like image 224
kaulex Avatar asked Aug 06 '20 19:08

kaulex


People also ask

What is single in KOIN Android?

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.

What is dependency injection Koin?

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.

What is KOIN single?

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.

How does Koin work?

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.


1 Answers

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()
like image 70
kaulex Avatar answered Sep 22 '22 13:09

kaulex