Piotr Prus
09/12/2023, 1:42 PM@ViewModelScoped
that we use in few places.
My usecase is as follows: I have a viewmodel and 2 usecases. These usecases are using (injecting) the same component. I dagger hilt I have these usecases and component with annotation @ViewModelScoped
, so even if usecase2 was created, the component class was not recreated. I am trying to achive the same thing in koin, but so far no luck.
More detailed schema:
class MyViewModel(private val usecase1, private val usecase2): ViewModel()
class UseCase1(private val myComponent)
class UseCase2(private val myComponent)
I tried with factory
on usecases and component, but then the component is created twice
I tried with single
on usecases and component, but then the component is created once in app lifecycle
I tried to make custom scope in compose that use this viewmodel, but I got error with Check your definitions
. Koin do not see my usecases 😞
What is the proper way to handle it? Please help. I need this migration to start with KMP Karnaud.giuliani
09/13/2023, 8:43 AMmyComponent
?Piotr Prus
09/13/2023, 9:30 AMmyComponent
has Factory annotation.
Here is the generated module:
scope(org.koin.core.qualifier.StringQualifier("myScope")) {
scoped() { MyComponent(get(),get(),get()) }
scoped() { UseCaseOne(get()) }
scoped() { UseCaseTwo(get()) }
}
viewModel { MyViewModel(get(), get()) }
Piotr Prus
09/13/2023, 9:34 AMval viewModel = koinViewModel<MyViewModel>(
scope = getKoin().createScope(
"myScope",
named("myScope")
)
)
and the usecases have the following annotations:
@Scope(name = "myScope")
@Scoped
class UseCaseOne (private val myComponent)
the myComponent also is scoped the same way:
@Scope(name = "myScope")
@Scoped
class MyComponent()
the ViewModel do not have the scope
annotation, just @KoinViewModel
arnaud.giuliani
09/13/2023, 4:09 PMPiotr Prus
09/13/2023, 4:11 PMarnaud.giuliani
09/13/2023, 4:12 PMarnaud.giuliani
09/13/2023, 4:12 PMarnaud.giuliani
09/13/2023, 4:13 PMarnaud.giuliani
09/13/2023, 4:13 PMarnaud.giuliani
09/13/2023, 4:13 PMPiotr Prus
09/13/2023, 4:19 PMPiotr Prus
09/13/2023, 4:22 PM@Scope(MyViewModel::class)
@KoinViewModel
class MyViewModel
but in this setup I got error: Check your definitions
for MyViewModel, which was weird. The generated module looks like that:
scope(MyViewModel) {
viewModel { MyViewModel(get(), get()) }
scoped() { MyComponent(get(),get(),get()) }
scoped() { UseCaseOne(get()) }
scoped() { UseCaseTwo(get()) }
}
Piotr Prus
09/13/2023, 4:23 PMarnaud.giuliani
09/13/2023, 4:32 PMarnaud.giuliani
09/13/2023, 4:32 PMColton Idle
09/21/2023, 4:37 AMPiotr Prus
09/21/2023, 7:47 AM@ViewModelScoped
, but for now I have workaround.arnaud.giuliani
09/22/2023, 7:21 AM@ViewModelScoped
arnaud.giuliani
09/22/2023, 7:23 AMOsman Saral
09/27/2023, 1:47 PMOsman Saral
09/27/2023, 2:47 PMOsman Saral
09/27/2023, 3:13 PMarnaud.giuliani
09/27/2023, 4:24 PMOsman Saral
09/27/2023, 5:06 PMarnaud.giuliani
12/06/2023, 9:18 AMmy app is a jetpack compose app with a single activity. don't you think it's a bad idea to relay on the activities lifecycle?in a default way, you don't really need scope. Depends yoru usage
arnaud.giuliani
12/06/2023, 9:19 AMPiotr Prus
01/05/2024, 7:27 AM