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

Francesco megna

07/08/2018, 8:05 PM
It’s a good idea to bind the entire viewModel class to a view instead specifics properties? In some cases not all property and methods of the viewModel are used by the view
🤔 1
l

louiscad

07/08/2018, 9:34 PM
I think it's not proper English, I have trouble understanding your question
f

Francesco megna

07/09/2018, 12:26 AM
Sorry, the English is not my native language, i edit the question, now should is more clear
e

edwardwongtl

07/09/2018, 3:33 AM
What do you mean by bind the entire viewModel to a view instead of specific properties?
f

Francesco megna

07/09/2018, 6:29 AM
For example: I need a liveData User for my view and the User is in the viewModel class, with other method and properties. Now i can bind the entire viewModel to view and get the user or bind only the user
g

gildor

07/09/2018, 10:01 AM
And what is question? it’s really depends on your case and about level of encapsulation that you expect. Of course if View depends on own ViewModel with only required properties and methods this sounds like a better approach, but sometimes it’s just overkill, it’s just a matter of practical approach But what is embarrasses me, is that you mentioned ViewModel, so this is MVVM, but in MVVM your view should know nothing about ViewModel, your binding should know how to apply VM state to View or update VM according to View state, so not sure what is your case when View has dependency on ViewModel
f

Francesco megna

07/09/2018, 1:28 PM
In the official doc of data binding the entire viewModel is attached to the view, so i think is not a violation of the architecture, my questions is only about the “performance”
g

gildor

07/09/2018, 4:18 PM
Could you show an example of official doc where view that have dependency on ViewModel? Maybe we are talking about different things I don't think there is any measurable performance difference, it's more about architecture
The first example of xml have the dependency of viewModel, but probably I’m wronging something, show me the truth, man 😅
g

gildor

07/09/2018, 4:31 PM
It's big documentation page, any particular examples?
View doesn't have any dependency on View model in this example
It's Binding layout not a view. View in this case is TextView class
Binding connects View and ViewModel (using binding adapters), but view know nothing about view model or even bindings library, ViewModel also don't know about view
So what do you mean when talking about binding of "entire viewmodel"?
f

Francesco megna

07/09/2018, 5:02 PM
Mh, so i missing something, for me the dependency is “<data name”viewModel” type=“com.data.viewModel” />”, the dependency is the type of the viewModel
and with “bind the entire viewModel” I mean this core, instead to bind specifics properties of the viewModel
so what is the real dependency to avoid?
g

gildor

07/09/2018, 5:03 PM
No, I'm just talking about dependency like "some class uses another class"
f

Francesco megna

07/09/2018, 5:03 PM
Ahh, so you mean to avoid call a dependency of viewModel from the view, right?
g

gildor

07/09/2018, 5:05 PM
As we already figure out I was confused because you called binding "view", but binding is not view
f

Francesco megna

07/09/2018, 5:07 PM
yes man, I confuse the terminology, thx for your patience
g

gildor

07/09/2018, 5:07 PM
So could you show some sample code that shows binding that uses "entire viewmodel" and properties. Just to be clear and understand which options do you concider