Jun 23 | 7:20 PM |
Mark M. | has entered the room |
Mark M. | turned on guest access |
Jun 23 | 7:45 PM |
ansh s. | has entered the room |
ansh s. |
hi
|
Mark M. |
hello, ansh~
|
Mark M. |
how can I help you today?
|
ansh s. |
Hello sir. I was working on architecture components and i am having troubles wrapping my head around the concepts of vewmodel and livedata and life cycles
|
Jun 23 | 7:50 PM |
ansh s. |
like what exactly are they? what problem are they solving? how can they be used singularily and in combination?
|
ansh s. |
also
|
Mark M. |
I have a lot of material on those subjects in *Elements of Android Jetpack*, *Exploring Android*, and *Android's Architecture Components*, all of which you can read as part of your Warescription
|
Mark M. |
so... could you be a bit more specific?
|
ansh s. |
yes i have been reading the architectures book. I didn't understood the lifecycles part. just wanted a starter . like am able to see that when used a combination of room+vm+livedata, we get a very good reactive app which automatically updates ui when items in db changes...
|
ansh s. |
but what is doing what here? i tried making a simple app with just the room, and it turned out okay, except i had to use a lot of threads and handlers here and there
|
Jun 23 | 7:55 PM |
ansh s. |
i also have this app code available to me, written with some good design pattern, idk . it uses multiple viewmodels for each activity
|
ansh s. |
i think i should read the books more before coming here
|
Mark M. |
you might have more than one ViewModel for an activity, where the ViewModel is shared with a fragment -- so, an activity with N fragments might need ~N ViewModels
|
Mark M. |
an activity is not *required* to have multiple ViewModels, but it is an option
|
ansh s. |
okay
|
ansh s. |
oh , i meant each activity had its own viewmodel
|
ansh s. |
so multiple viewmodels in an app
|
Mark M. |
that is reasonable, though there is a greater emphasis now on fewer activities and more fragments
|
Mark M. |
back to your simple app with just Room, it is possible that you skipped over certain problems that ViewModel and LiveData are here to help with, such as configuration changes
|
ansh s. |
oh okay
|
Mark M. |
ViewModel and LiveData are not required, just as fragments are not required, the Navigation component is not required, etc.
|
Mark M. |
we did a lot of Android development in the years before the Architecture Components were released
|
Mark M. |
however, the Architecture Components are useful, and they are Official Google Stuff :-)
|
Jun 23 | 8:00 PM |
ansh s. |
i think here is what i have understood( a little bit): livedata is just a variable which when changed would trigger some interfaces on the components that are observing it. and the viewodel is i think a way to handle problems regarding configuration changes and those problems that came with asynctask, idk??
|
Mark M. |
LiveData also know about configuration changes, in that observers are usually tied to LifecycleOwner objects, such as activities and fragments
|
Mark M. |
LiveData knows to deliver the current value to observers that, from a lifecycle standpoint, become started again
|
Mark M. |
this is very useful combined with ViewModels -- we just ask ViewModelProviders.of() for our ViewModel, then set up observers on the LiveData in that ViewModel
|
Mark M. |
it does not matter whether our activity/fragment was newly created, or whether it was re-created after a configuration change
|
Mark M. |
we just start observing the ViewModel-managed LiveData, and It Just Works(TM)
|
ansh s. |
ohh okay.
|
Jun 23 | 8:05 PM |
ansh s. |
so instead of our activity, its the viewmodel attached to our activity that handles the livedata, right? its like our activities' and other components' lifecycles are messed up, so viewmodel acts as the lifecycle provider to livedata: like if livedata was a guy who would give an activity the changes only when it is in its onCreate(), it would rather now deal with viewmodel who is saying "hey man, my friend is activity is in its oncreate(), you give me changes just now!" even when activity is not
|
ansh s. |
lol its a messed up analogy
|
Mark M. |
"so viewmodel acts as the lifecycle provider to livedata" -- I would not say that
|
Mark M. |
the ViewModel is a container for LiveData and other stuff that needs to survive a configuration change
|
Mark M. |
LiveData knows to push data to observers (typically activities/fragments) both when the data changes and when the observers lifecycle states change
|
Mark M. |
a ViewModel on its own knows very little about lifecycles -- it gets called with onCleared() once we know that it will no longer be used, but otherwise that's it
|
Jun 23 | 8:10 PM |
ansh s. |
So how is VM helping survive a configuration change when livedata knows the activity knows that activity is having a configuration change?
|
ansh s. |
I am sorry, you can totally say me to go read back the books first if am being too naive in my questions. i am only coming here after developing some sample apps and not reading the theory much
|
Mark M. |
well, I do recommend reading the stuff in *Elements of Android Jetpack* on this
|
Mark M. |
with respect to your question, the problem is garbage collection
|
Mark M. |
in Java/Kotlin, objects get garbage-collected if nothing holds a reference to them
|
Mark M. |
the LiveData has a reference to a LifecycleOwner (activity/fragment) and an Observer... but, by default, there is nothing holding a reference to the LiveData
|
Mark M. |
if the activity or fragment hold onto the LiveData, then when the activity or fragment gets destroyed on a configuration change, we no longer have that reference to the LiveData, so it can get garbage collected
|
Jun 23 | 8:15 PM |
Mark M. |
plus, the new activity/fragment has no way of getting the LiveData from the old activity/fragment
|
Mark M. |
that is where ViewModel comes in
|
Mark M. |
without getting into all the implementation details, ViewModels are designed to survive a configuration change
|
ansh s. |
ohh okay, like a service
|
Mark M. |
sort of
|
Mark M. |
it's complicated :-(
|
Mark M. |
also, a ViewModel is not going to be eligible for garbage collection until the activity associated with it is truly destroyed, not counting any configuration changes
|
Mark M. |
so, if a ViewModel holds the LiveData, not only will the new activity/fragment be able to access the LiveData from before, but the LiveData cannot be garbage-collected, because the ViewModel has a reference to it
|
ansh s. |
oh okay
|
Jun 23 | 8:20 PM |
ansh s. |
You know once i was trying to test the best way to do background processing. i thought of a simple app where on the press of a button there should be a for loop that should start counting to 1 billion and should send the value to my textview for each iteration. i tried many funny stuff, doing in seperate thread(crrashed) doing in main thread(crashed), doing with asynctask( got ugly when screen rotated) and finally making a bg service that would trigger an interface on every iteration. if my app is active , it would recieve the changes, and if it isn't the service would still go on
|
Mark M. |
that works, but services have their own issues, which is why Google keeps adding restrictions on them with each passing Android release
|
ansh s. |
VM sounds something like that, like the main thing that is throwing updates is held at some place where it is not effected weather an activity is active or in landscape or portrait , etc
|
Mark M. |
and its impact (ideally) is limited to the activity, unlike a service
|
ansh s. |
i think i got some good image of what's going on . thanks
|
ansh s. |
Also i asked a question on SO yesterday regarding many to many relation in room yesterday. it hasn't got any answers. can you or your team look into it?
|
Mark M. |
I don't have a team :-)
|
Mark M. |
it's just me
|
Mark M. |
do you have a link to the question?
|
Jun 23 | 8:25 PM |
ansh s. | |
Mark M. |
I will try to take a look at it tomorrow
|
Mark M. |
once a question is 24 hours old, you can go to the Warescription site and request a "bump" of the question
|
ansh s. |
and wow really? i thought there would be like 5-10 people behind the single name commonsware taking smaller doubts hehe
|
ansh s. |
yes sure thanks
|
ansh s. |
i was also wondering about the discussion forum here
|
Mark M. |
the site for it is https://community.commonsware.com/
|
Mark M. |
it can be read by anyone
|
Mark M. |
if you want to post to the Android Development category, you need to fill in a form on the Warescription site, after having set up an account on the community site
|
Mark M. |
(it is a bit of a hassle, sorry)
|
ansh s. |
its like 5 am here now. i am also most of the time unable to get here during the office hours. can i sometimes post my stuff there? i hope it isn't as strictly moderated as SO
|
Mark M. |
so long as you are an active subscriber, you will be welcome to post questions there
|
Mark M. |
depending on the subject, I may or may not be able to help
|
ansh s. |
yes i have got that accont stuff right. i recieved a mail with access permissions
|
Mark M. |
ah, yes, I see the mail that I sent you a few days ago
|
Mark M. |
so, you can ask questions in the Android Development category on that site
|
Jun 23 | 8:30 PM |
Mark M. |
and, that is a wrap for today's chat
|
Mark M. |
the transcript will be posted to https://commonsware.com/office-hours/ shortly
|
ansh s. |
oh okay
|
ansh s. |
thanks have a great day/night/evening
|
Mark M. |
you too!
|
ansh s. | has left the room |
Mark M. | turned off guest access |