guys, I have some doubts about how longer lives a ...
# koin
r
guys, I have some doubts about how longer lives a single instance and the result of a factory instances. I already understand that single=bean=a lazy instance that can be reused anytime that it’s needed, and factory is a factory of these beans = singles. My main doubt is, if I have a factory of usecases/interactors, how long will live usecase1, usecase2 etc? Is this always engaged with application scope? Or should I define a new scope to make my usecase lives until viewmodel.onCleared(e.g)?
a
a
single
is a singleton instance
a
factory
create an instance for you each time your request the definition
You can inject a
factory
definition into your `ViewModel`’s constructor as it will just request it the first time
I uses
factory
to declare my usecases classes
if you don’t inject the factory definition elsewhere, you will just have one instance created
r
if I don’t inject my
factory
into my viewmodels, my usecases instances will be engaged with the application lifecycle right?
and about
a factory create an instance for you each time your request the definition
, and how long lives each instance that was created each time to request the definition?
thanks in advance @arnaud.giuliani
a
in Koin, everything is lazy - and then nothing is created until you ask for it
for a
factory
definition, Koin create it each time you request it. But Koin won’t hold it for you:
you factory instance will be hold by the component that requested it
Copy code
class MyUseCase
class MyViewModel(val myUseCase : MyUseCase) : ViewModel()

module {
  factory { MyUseCase() }
  viewModel { MyViewModel(get())}
}
the MyViewModel instance will keep the MyUseCase instance until it is destroyed
r
👍 ok thanks