Android Jetpack: LiveData


at Google i/o 2017 we announced the life cycle library a set of libraries and guidance for avoiding memory leaks and solving common Android lifecycle challenges the life cycle library has hid 2.

0 it's also now part of jetpack and includes new integrations with data binding this is a tour of the lifecycle libraries live data class live data is an observable data holder class that is also lifecycle aware let's take a look at example you're gonna have your UI and then you're also gonna have this live data object which holds some data that you want to show on screen the UI observes the live data object this is like saying the UI wants to be notified of updates therefore when the live data changes the UI will get notified and then the UI can redraw itself with the new data so in short live data makes it easy to keep what's going on screen and synch with the data ok so here's some actual code live data objects will usually be kept in the view model class if you're not sure what a view bubble is no worries check out the video let's say you're creating an activity and view model for a user profile you'll have this user live data object that holds a user object now over in your activities on create you'll get that live data from the view model class call observe on the live data for the first argument you're gonna pass in the UI in this case the activity the second argument is a observer which is just a callback here you will call the code to update the UI now you can change the live data by calling either set value or post value use set value if you're running on the UI thread and then use post value if you're running on a background thread when either set value or post value is called the live data notifies active observers if you're using Android studio 3.

1 or higher live data and view models work with data binding usually you're going to go ahead and bind your view model to your XML layout which is explained in the view model video now after associating your view model and data binding layout you just need to add the single line change to have your live data be properly observed when bound to the XML you can now include references in your XML to your view model and the live data stored within it if you use data binding you're going to no longer need to actually Mamie Li set up these observers so instead of creating the live data observer code that I showed you before you could remove all that boilerplate instead the text views xml references that live data directly what makes live down to different from other observables is that it is also lifecycle aware this means that it understands whether your UI is on screen off screen or destroyed live data knows about your UI state because you pass it in when you call observe okay so here are some benefits of live datas lifecycle awareness so let's say your activity is not on screen then your live data doesn't trigger useless UI updates if the activity or UI gets destroyed that the observation connection is cleaned up automatically by live data thus you'll never accidentally trigger an activity or fragment that is off screen or destroyed to redraw itself this is possible in part because of interfaces and classes in the lifecycle library that are also used by framework classes these classes are life cycle which is an object that represents an android lifecycle and what state it's in lifecycle owner which is an interface for objects that have a lifecycle like app compat activity or an activity fragment and finally lifecycle observer which is an interface for observing a lifecycle okay so live data is a lifecycle observer it abstract away the need for you to deal directly with activity or fragment lifecycle so those are the basics of working with live data and why it's useful I'm going to touch on a few more complex usages room is built to work well with live data room can return live data objects which are automatically notified when the data base data changes and have their data loaded in a background thread this makes it easy to have the UI update when your database updates you can go ahead and learn more about room in the introduction to room video live data also provides transformations including map switch map and a class called mediator live data for your own custom transformations map lets you apply a function to the output of live data a and then propagate the results down straight to live data B for example you can use live data to take a user object and instead output a string of the users combined first and last name the switch map function transformation is a lot like map but for mapping functions that emit live data instead of values so an example here is if you have a bunch of users perhaps stored in a room you might have a lookup function for those users using switch map you'd have a live data for the user ID whenever the ID changes your user lookup function would be called with that ID the results live data now reference is the newly found user live data okay so no matter how many different times you call this lookup function again a different live data your UI only needs to observe the result live data at once which is the power of switch map now if you want to go ahead and make your own custom data transformations you should take a look at the mediator live data class mediator live data includes methods to add and remove source live data objects you can then combine and propagate events from all of these sources downstream getting started with live data is simple but there is a lot of potential for experimentation with this life cycle where observable hopefully this is inspired you to build something and as always the documentation is linked.

Leave A Reply

Your email address will not be published.