-
-
Notifications
You must be signed in to change notification settings - Fork 725
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
Order of onClose calls #1510
Comments
Yes, Koin will unload things in order of its index. The onClose function could be predicted to a common component to help you clear resources. A scope closure could help you trigger one function to close your resources. I imagine here, more a shutdown process for the overall Koin app, no? 🤔 |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
I am running into the same problem as @smichovan where services are closed in no specific order. What is the recommended way to close services in the example that @smichovan provided? A short code example would be greatly appreciated. |
I think that Koin should automatically infer order from dependencies relation. Plus it could throw an error in case there is a cyclic depenency so the orded is not possible to predict, the same as with dependency injection. |
About predicting of order I'm also out in blue. My code is as follows:
Each of them registers a
It doesn't change if I switch order of |
Discussed in #1507
Originally posted by smichovan January 20, 2023
Is there a way how to change order in witch the onClose methods of components are called?
Lets have two services where the latter is dependent of the former:
val appModule = module {
single { Database() } onClose {it?.closeConnectionPool()}
single { ServiceUsingDb(get()) } onClose {it?.useDatabaseForCleanup()}
}
Database component maintains a connection to some DB and the second component accesses the DB through the first component. The "ServiceUsingDb" needs to have DB available in onClose method. As the "ServiceUsingDb" is actually dependent on "Database" I would expect Koin to ensure the right closing order.
I made some research in the Koin code and if I understand it well, the components (instances, mappings) are stored in HashMap, means no specific order is guaranteed. In my case, it may (and does) happen that when ServiceUsingDb onClose is called, the DB is already closed and the cleanup fails.
Have I missed anything? Is there any way how to influence the onClose methods order?
The text was updated successfully, but these errors were encountered: