Office Hours — Today, June 23

Thursday, June 20

Jun 23
7:20 PM
Mark M.
has entered the room
Mark M.
turned on guest access
7:45 PM
ansh s.
has entered the room
ansh s.
hi
Mark M.
hello, ansh~
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
7:50 PM
ansh s.
like what exactly are they? what problem are they solving? how can they be used singularily and in combination?
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
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...
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
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
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
an activity is not *required* to have multiple ViewModels, but it is an option
ansh s.
okay
oh , i meant each activity had its own viewmodel
so multiple viewmodels in an app
Mark M.
that is reasonable, though there is a greater emphasis now on fewer activities and more fragments
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.
we did a lot of Android development in the years before the Architecture Components were released
however, the Architecture Components are useful, and they are Official Google Stuff :-)
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
LiveData knows to deliver the current value to observers that, from a lifecycle standpoint, become started again
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
it does not matter whether our activity/fragment was newly created, or whether it was re-created after a configuration change
we just start observing the ViewModel-managed LiveData, and It Just Works(TM)
ansh s.
ohh okay.
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
lol its a messed up analogy
Mark M.
"so viewmodel acts as the lifecycle provider to livedata" -- I would not say that
the ViewModel is a container for LiveData and other stuff that needs to survive a configuration change
LiveData knows to push data to observers (typically activities/fragments) both when the data changes and when the observers lifecycle states change
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
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?
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
with respect to your question, the problem is garbage collection
in Java/Kotlin, objects get garbage-collected if nothing holds a reference to them
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
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
8:15 PM
Mark M.
plus, the new activity/fragment has no way of getting the LiveData from the old activity/fragment
that is where ViewModel comes in
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
it's complicated :-(
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
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
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
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 :-)
it's just me
do you have a link to the question?
8:25 PM
ansh s.
Mark M.
I will try to take a look at it tomorrow
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
yes sure thanks
i was also wondering about the discussion forum here
Mark M.
it can be read by anyone
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
(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
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
so, you can ask questions in the Android Development category on that site
8:30 PM
Mark M.
and, that is a wrap for today's chat
the transcript will be posted to https://commonsware.com/office-hours/ shortly
ansh s.
oh okay
thanks have a great day/night/evening
Mark M.
you too!
ansh s.
has left the room
Mark M.
turned off guest access

Thursday, June 20

 

Office Hours

People in this transcript

  • ansh sachdeva
  • Mark Murphy