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

Crash in Koin 3.2.1: "SavedStateProvider with the given key is already registered" #1434

Closed
jafar-aml opened this issue Sep 19, 2022 · 3 comments
Assignees
Labels
android status:checking currently in analysis - discussion or need more detailed specs type:issue
Milestone

Comments

@jafar-aml
Copy link

Describe the bug
We are getting some errors, after upgrading to Koin 3.2.1.

Fatal Exception: java.lang.RuntimeException Unable to destroy activity {com.e*.***/com.e*.***.ui.main.MainActivity}: java.lang.IllegalArgumentException: SavedStateProvider with the given key is already registered androidx.savedstate.SavedStateRegistry.registerSavedStateProvider (SavedStateRegistry.kt:110) androidx.lifecycle.ViewModelProvider.get (ViewModelProvider.kt:153) org.koin.androidx.viewmodel.ViewModelResolverKt.resolveInstance (ViewModelResolver.kt:15) org.koin.androidx.viewmodel.scope.ScopeExtKt.getViewModel (ScopeExt.kt:67) org.koin.androidx.viewmodel.scope.ScopeExtKt.getViewModel (ScopeExt.kt:53) org.koin.androidx.viewmodel.scope.ScopeExtKt.getViewModel$default (ScopeExt.kt:45) org.koin.androidx.viewmodel.ext.android.FragmentExtKt.getSharedViewModel (FragmentExt.kt:59) com.e*.***.ui.contentList.ContentListFragment$special$$inlined$sharedViewModel$default$8.invoke (FragmentExt.kt:61) com.e*.***.ui.contentList.ContentListFragment$special$$inlined$sharedViewModel$default$8.invoke (FragmentExt.kt:39) kotlin.UnsafeLazyImpl.getValue (Lazy.kt:81) com.e*.***.ui.contentList.ContentListFragment.getExamFilesViewModel (ContentListFragment.kt53) com.e*.***.ui.contentList.ContentListFragment.resetAllFilters (ContentListFragment.kt:201) com.e*.***.ui.contentList.ContentListFragment.onDestroy (ContentListFragment.kt:449)

To Reproduce
Despite our best efforts, we have not been able to reproduce the issue.

Expected behavior
The app should not crash.

Koin project used and used version (please complete the following information):
implementation "io.insert-koin:koin-android:3.2.1" implementation "io.insert-koin:koin-annotations:1.0.2" ksp "io.insert-koin:koin-ksp-compiler:1.0.2"

Additional moduleDefinition
Usage of viewModel: private val examFilesViewModel: ExamFilesViewModel by sharedViewModel()

marekdef pushed a commit to marekdef/InsertKoinSharedViewModel that referenced this issue Oct 5, 2022
@marekdef
Copy link

marekdef commented Oct 5, 2022

I have similar issue, with koin 3.2.0.
What I was trying to do was to create a Fragment that contain 3 subfragments and I want to share viewModel with them.

I created reproducable repository here
https://github.com/marekdef/InsertKoinSharedViewModel

It crashes on launch when trying to setup 5 viewmodels that should be shared.

what I noticed is that if I don't use
clazz = MainViewModel::class in Fragment.getSharedStateViewModel then the project works fine (in fragment).

So commenting out that fixes the problem (in the repo provided)

In koin 3.2.1, 3.2.2 even using this clazz works so not sure if you have the same problem as me.

As of koin 3.2.1+ the Activity.getSharedStateViewModel() does not allow clazz parameter (in activity).

@arnaudgiuliani
Copy link
Member

will look at your repo @marekdef

@arnaudgiuliani arnaudgiuliani self-assigned this Oct 11, 2022
@arnaudgiuliani arnaudgiuliani added the status:checking currently in analysis - discussion or need more detailed specs label Oct 11, 2022
@arnaudgiuliani arnaudgiuliani modified the milestones: 3.2.3, 3.3.0, android-3.3.0 Oct 18, 2022
@arnaudgiuliani
Copy link
Member

The VIewModel API has been revamped with the latest lifecycle library in 2.5.1. #1459

More details on Google API: https://developer.android.com/jetpack/androidx/releases/lifecycle#2.5.1

Don't hesitate to give feedback on koin-android 3.3.0 🙏

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

No branches or pull requests

3 participants