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

URGENT!!! (KoinViewModel doesn't scope properly for Lifecycle Changes on Android (For Kotlin Multiplatform) #1944

Open
TheArchitect123 opened this issue Aug 12, 2024 · 5 comments
Labels
status:checking currently in analysis - discussion or need more detailed specs status:wait_feedback viewmodel
Milestone

Comments

@TheArchitect123
Copy link

Describe the bug
Currently I'm trying to load a view model into a fragment, which is using android navigation.
The issue here is that if I try to load a view model annotated with KoinViewModel, if the fragment/activity gets destroyed, and the lifecycle changes, the ViewModel recreates a new instance of itself, and it causes the whole process to break.

class MyViewModel : ViewModel() {
     
}

// Inside my fragment/activity
val viewModel = ViewModelCompat.getViewModel(this, MyViewModel::class.java)

// I tried this as well, same issue
val viewModel : MyViewModel by viewModels()

If I change orientation, or if the fragment gets destroyed, a new instance of MyViewModel gets recreated, and it causes an issue because the app has subscriptions/workers running on initialisation.

Expected behavior
What I expect is that the view model itself survives configuration changes, across the lifecycle. However it's not been scoped properly, and I'm forced to use Singleton Annotations for ViewModels, and manually do cleanup/deinit for any resources

Koin module and version:
implementation("io.insert-koin:koin-android:3.5.6")
implementation("io.insert-koin:koin-core:3.5.6")

Snippet or Sample project to help reproduce
This is a commercial project, so I can't upload the source code here.

@MaxMichel2
Copy link

This seems different in some aspect but also similar to my issue described in #1940. I'd be curious if you could confirm whether or not these two are similar since if they are, the root cause of both could be the same (making a fix simpler to identify and do)

@TheArchitect123
Copy link
Author

TheArchitect123 commented Aug 13, 2024

The issue does look similar yes.

@MaxMichel2

@TheArchitect123
Copy link
Author

@arnaudgiuliani

@arnaudgiuliani
Copy link
Member

If you have any way to reproduce it (a sample project), this would clearly help me to fix this 👍

@arnaudgiuliani arnaudgiuliani added status:checking currently in analysis - discussion or need more detailed specs viewmodel labels Aug 27, 2024
@arnaudgiuliani arnaudgiuliani added this to the 4.0-RC2 milestone Aug 27, 2024
@arnaudgiuliani
Copy link
Member

arnaudgiuliani commented Aug 27, 2024

val viewModel : MyViewModel by viewModels()

The main Google API is behaving the same way?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:checking currently in analysis - discussion or need more detailed specs status:wait_feedback viewmodel
Projects
None yet
Development

No branches or pull requests

3 participants