InsertKoinIO / koin

Koin - a pragmatic lightweight dependency injection framework for Kotlin & Kotlin Multiplatform
https://insert-koin.io
Apache License 2.0
8.81k stars 700 forks source link

2.2.1 -> 2.2.2 regression: No definition found for SavedStateHandle #1009

Closed gmk57 closed 3 years ago

gmk57 commented 3 years ago

Describe the bug I'm injecting ViewModel with SavedStateHandle, as described in the docs and the blog post about new API in Koin 2.2:

class MyStateViewModel(val state: SavedStateHandle) : ViewModel()

val appModule = module {
    viewModel { MyStateViewModel(get()) }
}

class MainActivity : AppCompatActivity(R.layout.activity_main) {
    val myStateVM: MyStateViewModel by viewModel()
}

This works in Koin 2.2.1, but in 2.2.2 it crashes with NoBeanDefFoundException: No definition found for class:'androidx.lifecycle.SavedStateHandle'. Check your definitions!

To Reproduce Check out my sample project

Koin project used and used version: koin-androidx-viewmodel version 2.2.2

This bug is already mentioned in other issues related to 2.1 -> 2.2 migration, but i think it deserves a thread of its own.

Side note: changelog is missing for 2.2.* versions.

DJDarkByte commented 3 years ago

Just ran into this problem as well. I think the problem is in ViewModelResolver.kt:

private fun <T : ViewModel> Scope.pickFactory(
        viewModelParameters: ViewModelParameter<T>,
): ViewModelProvider.Factory {
    return when {
        viewModelParameters.registryOwner != null && viewModelParameters.initialState != null -> StateViewModelFactory(this, viewModelParameters)
        else -> DefaultViewModelFactory(this, viewModelParameters)
    }
}

I tried setting an initial state with emptyState() to make sure that viewModelParameters.initialState != null, and that fixed it. The documentation doesn't mention this though.

The check for viewModelParameters.initialState was added in commit 47d120c but wasn't there previously.

hoc081098 commented 3 years ago

https://github.com/Kotlin-Android-Open-Source/MVI-Coroutines-Flow/blob/2419ff9e533fdf3d6e42ebdb82b563ac051d66ea/feature-add/src/main/java/com/hoc/flowmvi/ui/add/AddModule.kt#L15 I have used it.get() instead 😃 and it works

viewModel {
    AddVM(
      addUser = get(),
      savedStateHandle = it.get(),
    )
  }
rewgoes commented 3 years ago

@arnaudgiuliani, can you please check this issue? Is there any change that was not documented or this is an actual issue on version 2.2.2?

rewgoes commented 3 years ago

@hoc081098

https://github.com/Kotlin-Android-Open-Source/MVI-Coroutines-Flow/blob/2419ff9e533fdf3d6e42ebdb82b563ac051d66ea/feature-add/src/main/java/com/hoc/flowmvi/ui/add/AddModule.kt#L15 I have used it.get() instead 😃 and it works

viewModel {
    AddVM(
      addUser = get(),
      savedStateHandle = it.get(),
    )
  }

That didn't work for me! :expressionless:

arnaudgiuliani commented 3 years ago

Koin 2.3.0 will bring back separated API for StateViewModel. Documentation is a bit out of date. Just need to finish website and maven central migration.

arnaudgiuliani commented 3 years ago

@arnaudgiuliani, can you please check this issue? Is there any change that was not documented or this is an actual issue on version 2.2.2?

Try to use injected parameters like: {params -> MyViewModel(params.get())}, to resolve injected params.

Koin 3.0.1 will restore a consistent saveStateViewModel() API

arnaudgiuliani commented 3 years ago

Koin 2.3.0 will backport new APIs from 3.0.1 to 2.2.x branch

bartoliniii commented 3 years ago

Hi, I'm looking for workaround and I'm not able to find any. Using it.get() cause me a little different error (on both 2.2.2 and 2.2.1 versions):

org.koin.core.error.DefinitionParameterException: No value found for type 'androidx.lifecycle.SavedStateHandle'

Is there any way to make it working without migration to 2.1.x version?

arnaudgiuliani commented 2 years ago

for now, we don't expect to fix 2.x. Koin 3.x will be eligible for LTS support.

arnaudgiuliani commented 2 years ago

If you need more help, come here: https://github.com/InsertKoinIO/koin/issues/1212