Osman Saral
12/11/2023, 12:26 PMKMMViewModel
as base class (which also extends from androidx ViewModel)
Are there any difference between viewModelOf
and factoryOf
in terms of implementation? I couldn't see any difference when I looked at the source code. But I couldn't understand why viewModelOf
exists in the first place, if they're the sameMarcello Galhardo
12/13/2023, 5:02 PMviewModelOf
will be wired with a ViewModelProvider
when you use the correct viewModel
function to get an instance (either with Compose or not; scoping in the ViewModelStore
).
For example: if you retrieve a viewModel
3 times in an Activity
, you will receive the same instance.
If you use factoryOf
, each call will return a new instance - in the previous example, you would get 3 newly created objects.Marcello Galhardo
12/13/2023, 5:06 PMMarcello Galhardo
12/13/2023, 5:07 PMViewModel
works on Android. Please see the official docs here: https://developer.android.com/topic/libraries/architecture/viewmodel#lifecycleOsman Saral
12/13/2023, 6:31 PMViewModel
works. Thanks 🙂 I wasn't aware that my question made me look like a beginner 😅 It's okay though.
I'm already resolving the dependency with getViewModel
from compose. Is it the resolving the dependency that wires the ViewModel
with a ViewModelProvider
or is it defining the dependency.
I mean, are those differ:
viewModelOf
-> getViewModel
factoryOf
-> getViewModel
If they do, can you show me how viewModelOf
wires with a ViewModelProvider
in the source code? Just for educational purposes 😊 (Cuz, the Module.viewModel
function in ModuleExt.kt
only calls the factory
function)Marcello Galhardo
12/14/2023, 5:48 PMviewModelOf
only accepts ViewModel
, factoryOf
accepts Any?
. Scope wise, you are correct. Thanks for pointing it out.
The ViewModelProvider
wiring happens here.
KoinViewModelFactory doesn’t seem to check how the viewModel
was wired. That is a design flaw IMO.
Koin should:
(1) Only plug ViewModel
dependencies declared with `viewModelOf`/`viewModel` in a KoinViewModelProvider
.
(2) Koin should have an actual “view model scope”, where dependencies can be safely scoped during the life time of a view model. Right now, that doesn’t seem to be possible.
(3) KoinViewModelFactory should ensure the contract: only viewModelOf
dependencies can be provided.
That should ensure improved safety and better memory scoping for dependencies that aren’t reused between view models.
What do you think @arnaud.giuliani?arnaud.giuliani
12/15/2023, 8:54 AMviewModel
viewModelOf
keywords and `by viewModel()`functions are checking for VM types. But it might be open, to let you inject on an interfacearnaud.giuliani
12/15/2023, 8:55 AMfactory
instances into ViewModel those instances will follow the VMarnaud.giuliani
12/15/2023, 8:56 AMfactory
on native side and viewModel
on android side, to let this last wire on Android lifecyclearnaud.giuliani
12/15/2023, 8:57 AMOsman Saral
12/15/2023, 12:20 PMviewModelOf
and factoryOf
when I resolve the dependency using getViewModel
Doesn't this mean I don't have to use viewModelOf
on the Android side? I wanted to declare dependencies on the shared module with factoryOf
so I don't have to declare dependencies twice for iOS and Android side. Since I resolve the dependency using the getViewModel
on the Android side, it should be fine to use factoryOf
arnaud.giuliani
12/15/2023, 1:59 PMviewModel
keyword is behind the scene a Factory. But yeah, improvements should help about that later 👍