https://kotlinlang.org logo
#koin
Title
# koin
o

Osman Saral

12/11/2023, 12:26 PM
Hi, I'm moving my ViewModels to a shared module. And I'm going to use
KMMViewModel
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 same
m

Marcello Galhardo

12/13/2023, 5:02 PM
viewModelOf
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.
You might also want to learn more about how
ViewModel
works on Android. Please see the official docs here: https://developer.android.com/topic/libraries/architecture/viewmodel#lifecycle
o

Osman Saral

12/13/2023, 6:31 PM
Hi, I surely know how
ViewModel
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)
m

Marcello Galhardo

12/14/2023, 5:48 PM
viewModelOf
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?
a

arnaud.giuliani

12/15/2023, 8:54 AM
Yep interesting @Marcello Galhardo about injecting ViewModel type,
viewModel
viewModelOf
keywords and `by viewModel()`functions are checking for VM types. But it might be open, to let you inject on an interface
I believe it's quite limited about ViewModel injection scope, as finally for example injecting
factory
instances into ViewModel those instances will follow the VM
regarding KMP side, you should use
factory
on native side and
viewModel
on android side, to let this last wire on Android lifecycle
but else yes, there is interesting improvements to bring around VM
o

Osman Saral

12/15/2023, 12:20 PM
@arnaud.giuliani as @Marcello Galhardo confirmed, there is no difference between
viewModelOf
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
a

arnaud.giuliani

12/15/2023, 1:59 PM
viewModel
keyword is behind the scene a Factory. But yeah, improvements should help about that later 👍
1