InsertKoinIO / koin

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

ViewModel with MutableSharedFlow creation error #1877

Open arturdanielyan1 opened 1 month ago

arturdanielyan1 commented 1 month ago

In the ViewModel I have this:

private val loadSongsRequest = MutableSharedFlow<Unit>(
        onBufferOverflow = BufferOverflow.DROP_OLDEST
)

Requesting this ViewModel like this in a composable function:

val vm = koinViewModel<MyViewModel>(
    key = MyViewModel::class.simpleName
)

throws an exception: Process: com.nightx.ingale, PID: 14374 org.koin.core.error.InstanceCreationException: Could not create instance for '[Factory:'MyViewModel']'

The second I remove the line:

onBufferOverflow = BufferOverflow.DROP_OLDEST

everything works perfectly fine.

Also works when I create the MutableSharedFlow lazily:

private val loadSongsRequest by lazy {
        MutableSharedFlow<Unit>(
            onBufferOverflow = BufferOverflow.DROP_OLDEST
        )
    }

Behaviour is the same in versions 3.5.3 and 3.5.6

arturdanielyan1 commented 1 month ago

It turns out I didn't know that MutableSharedFlow with onBufferOverflow strategy DROP_OLDEST must have a positive extraBufferCapacity, but if that's the reason of the InstanceCreationException I think this is still a bug in Koin library as the error message doesn't tell anything related to the actual error. I unserstand that this happens at the ViewModel intialization time but as I said the error message tells completely different thing.