Hi, i have question regarding to *Scoping*: *Desir...
# koin
j
Hi, i have question regarding to Scoping: Desired: Create scope for ViewModel, should be created when ViewModel is created, released in
ViewModel#onCleared()
(not Activity lifecycle) Aim: ViewModel injects
UseCaseA
and
UseCaseB
, and both use cases inject
RealTimeConnection
interface. Implementation of
RealTimeConnection
injects
ClassC
. I want
RealTimeConnection
implementation and
ClassC
to be created only once in ViewModel scope. I have tried:
Copy code
module("app") {
    scope(SCOPE_VM) {
        ClassC()
    }
    scope(SCOPE_VM) { RealTimeConnectionImplementation(get()) as RealTimeConnection }

    factory { UseCaseA(get()) }  // parameter is RealTimeConnection
    factory { UseCaseB(get()) }  // parameter is RealTimeConnection
}
Question: i can not inject
UseCaseA
and
UseCaseB
from constructor of ViewModel because scope is created in
init{}
block of ViewModel. I can not use
by inject, get<>
inside ViewModel. What is the solution? Thanks
Or is there any better approach?