-
-
Notifications
You must be signed in to change notification settings - Fork 727
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
SingleOf does not generate truly singleton objects #2078
Comments
I don't think it's related to Koin here. How do you restart your app/them? |
In Hilt, if we mark a class as Singleton, then the instance is same as long as the app process is active. It should be same for Koin too? If a configuration change happens, then a new object should not be created.
I've created a sample android project to highlight this here: https://github.com/ksharma-xyz/KoinSingletonBug
|
You are having scope issues, you are creating a koin instance too the activity scope. That's why it's recreated on configuration changes. Start your koin instance in the Application class instead. Change MainApplication & KoinApplication.
And replace with KoinApplication with KoinAndroidContext
See Docs here |
Thanks! @afTrolle That worked for an android application. Do you have any suggestions for a ComposeMultiplatform Application? Seems like setting android context in
startKoin {
androidContext(this@KrailApplication) // not enough as KoinApplication also expects androidContext.
} |
It might depend on which platform/target we are talking about. I'm no expert in koin. But you could probably use previous solution for the others. In my iOS code I do similar to android application but call function init function when app starts. But that was just convient because I'm also starting up crashlytics from swift. |
Any updates on this @arnaudgiuliani. The issue is for Compose Multiplatform. |
If you want to start Koin before Compose, you need to use |
Describe the bug
When navigating between screens (
ScreenA
toScreenB
) that useViewModelA
andViewModelB
, and then switching between dark/light themes, theRepo
object injected intoViewModelB
is recreated. This behavior occurs despite definingRepo
as a singleton in the Koin module. As a result,ViewModelB
does not share the sameRepo
instance asViewModelA
, which breaks the expected singleton behavior. Based on compose multiplatform project.To Reproduce
Steps to reproduce the behavior:
ScreenA / ViewModel A and ScreenB / ViewModel B
Expected behavior
ViewModelB
should have the sameRepo
object as is present insideViewModelA
Koin module and version:
Snippet or Sample project to help reproduce
Add a snippet or even a small sample project to help reproduce your case.
Creating viewmodel like this in composable scope.
The text was updated successfully, but these errors were encountered: