https://kotlinlang.org logo
#android
Title
# android
c

Cake

12/11/2019, 12:44 PM
Hey guys, i was wondering why we can't inject the Viewmodel directly, but instead we've to inject a factory. Which ate the benefits came from this practice?
🤖 1
l

LeoColman

12/11/2019, 12:45 PM
Why can't you inject view model directly? I do it all the time
The benefits, in theory, is decoupling the ViewModel creation to the view model usage
Because if you ever want to change how the viewmodel is created, you only need to change the factory, and not the classes that create a view model
Suppose you have 20 classes that inject
new ViewModel()
If you change that to
new ViewModel(foo)
, you'll have to change 20 classes
If you use the factory, you'll have to change only the factory
But this isn't a rule, you aren't obligated to comply with every single bit of clean architecture/clean code
m

Matej Drobnič

12/11/2019, 12:59 PM
If you inject ViewModel then it will be recreated on every configuration change
👆 3
point of the factory is that it can create new VM, but it doesn't have to (it can reuse old one)
c

Cake

12/11/2019, 1:00 PM
Hmm i get it. But we had a problem few days ago with it. We were testing a Video feature and the tester rotated the screen, this action recreated the VM. We tested binding a scope in it, but same problem
l

LeoColman

12/11/2019, 1:00 PM
I don't think it's recreated necessarily. It depends on how you inject the vm
For instance, with
Koin
it won't be recreated
c

Cake

12/11/2019, 1:01 PM
Koin better than Dagger hahah. I'm joking guys, please do not kill me 😞 We were using Dagger by the way
m

Matej Drobnič

12/11/2019, 1:27 PM
we inject the factory via dagger and then ViewModelProviders does the rest
factory is created by dagger so there is no extra manual work
c

Cake

12/11/2019, 1:31 PM
Hmm thanks @Matej Drobnič and @LeoColman. One last doubt, do you have an example how do you implement this factory? The way that we're doing the Factory is being recreated every time that he is injected. We're using this tutorial as guide: https://android.jlelse.eu/android-less-dagger-boilerplate-code-for-viewmodel-creation-9fec91a27bf3
m

Matej Drobnič

12/11/2019, 1:32 PM
If you are using this tutorial, then factory is not recreated every time since it's
@Singleton
c

Cake

12/11/2019, 1:36 PM
Hmm, we're implementing the second way. I'm going to discuss the first way with my team and consider the tradeoff. Thanks @Matej Drobnič
m

Matej Drobnič

12/11/2019, 1:37 PM
We are actually doing it the second way too
it is much simpler and allows for parameters
I don't think one object created per activity is that big of a deal
c

Cake

12/11/2019, 1:38 PM
Hmm, we're considering to use Lazy initialization to solve this kind of problem.
m

Matej Drobnič

12/11/2019, 1:44 PM
where would you put the lazy?
activity gets destroyed
c

Cake

12/11/2019, 1:48 PM
Hmm one point to consider. We were thinking in fragment case, but we can retrieve using by viewModels. Howsoever we are going to use as described in tutorial.
m

Matej Drobnič

12/11/2019, 1:50 PM
I think you are prematurely optimizing this. It's one allocation per activity creation, it's not big enough deal to warrant such hacks.
c

Cake

12/11/2019, 1:51 PM
Probably, thanks for the feedback 😁
3 Views