Skip to content
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

KoinAppAlreadyStartedException after closing the app and relaunching it quickly #1864

Open
armond-avanes opened this issue May 1, 2024 · 2 comments

Comments

@armond-avanes
Copy link

Describe the bug
I'm using Koin in my Compose Multiplatform project and trying to keep as much as possible in commonMain, so I'm using KoinApplication(...) in my top layer composable function.

When I close the app (Android) and relaunch it quickly, I get KoinAppAlreadyStartedException. I believe that when I close the app, the activity is destroyed but the app itself is still alive for a couple of more seconds... in that case, when I relaunch it quickly, the app itself is not reinitiated (already exists) but the activity is recreated (because it has been destroyed). When KoinApplication(...) is called and it detects that the Koin has already been started in the past, it throws the exception.

I know I can make use of KoinContext() and instead start the Koin in "Application.onCreate()" to fix this issue. But then I will need to repeat a similar logic for iOS and Desktop which is what I'm trying to avoid.

To Reproduce
Steps to reproduce the behavior:

  1. Close the app
  2. Quickly relaunch it
  3. See error

Expected behavior
KoinApplication should handle this situation gracefully.

Koin module and version:

  • koin-core:3.6.0-Beta2
  • koin-compose:1.2.0-Beta2
@Nek-12
Copy link

Nek-12 commented May 24, 2024

By starting Koin in composition you absolve yourself of any chance of injecting dependencies outside composition. The correct approach is to start Koin in the application and make a function in shared module that starts koin and call it from swift.

@armond-avanes
Copy link
Author

@Nek-12 I understand your point, but KoinApplication() is a valid Koin [composable] API and it's not just a hacky way of doing things. If there is an API for something, it should work. Otherwise the API can be marked as deprecated, and the documentation should be updated to explain that there is no platform independent way to start Koin in Compose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants