I am trying to upgrade my koin usage from 2.1.6 -> 3.0.2 and am having troubles with the scoped injections.
I have MVPs where the Activity/Fragment is the view and i want to inject the view in the presenter.
so i have
module {
scope(named<MainActivity>()) {
scoped<View> { getSource() }
scoped<Presenter> {
MainPresenter(
view = get()
)
}
}
in 2.1.6 i used to used to do this and all was fine:
class MainActivity :
AppCompatActivity(),
MainContract.View {
private val presenter: MainContract.Presenter by currentScope.inject()
...
}
and then in MainActivity i NOW have:
class MainActivity :
AppCompatActivity(),
MainContract.View,
AndroidScopeComponent {
override val scope : Scope by activityScope()
private val presenter: MainContract.Presenter by scope.inject()
...
}
and Presenter:
class MainPresenter(
private val view: MainContract.View
){
...
}
but it cannot get the source object and i get the error:
Instance creation error : could not create instance for [Single:'uk.co.sentinelweb.cuer.app.ui.main.MainContract$View',scope:q:'uk.co.sentinelweb.cuer.app.ui.main.MainActivity']: java.lang.IllegalStateException: Can't use Scope source for uk.co.sentinelweb.cuer.app.ui.main.MainContract$View - source is:null
(i.e. when it tries to create the presenter it can't find the scoped MainActivity)
this is the existing code (using 2.1.6) https://github.com/sentinelweb/cuer/blob/develop/app/src/main/java/uk/co/sentinelweb/cuer/app/ui/main/MainActivity.kt
Have i got a lot more re-writing to do here? I am struggling to find a good example for scoped injection in the koin docs and a lot of it seems old. A lot of projects seem not to use scoping.
So if anyone can tell me what wrong here or point me to a decent example of something similar id appreciate it a lot!
So it seems for the lifecycle aware extension methods it just doesnt set the scope variable - perhaps they are paranoid about mempory leaks but since the scope is cleared and on the destroy lifecycle method - this shouldnt be a problem really.
My solution was to just make new extension methods which actually just pass in the source - I am not sure why this would be a problem. There is an issue here for it https://github.com/InsertKoinIO/koin/issues/851