‫If we look at the run method of the runnable, we can see that the returning type is void.
‫This means that it will return nothing.
‫If we go to get all contacts method.
‫We can't execute this in the background because at first the run will not return anything and I need
‫to return a list of contacts object.
‫And the second reason is that I need to publish contacts to get all contacts and publish it to the UI.
‫So this is will be executed in the background and the add contact contact.
‫The insert method will be executed on a separate thread and in the background.
‫But this I need to be executed and published the UI with the new contacts received from the database.
‫So for that I'll use the live data.
‫Live data is often used in both the repository and the Dao to facilitate the observation of database
‫changes and provide real time updates to the UI.
‫The first step is to go to the Dao and I need to update this list.
‫I'll encapsulate this list inside the live data object of type list of contacts objects.
‫Import the class of the live data.
‫When you have methods that are annotated with query and you are going to display something using the
‫select operator in SQL.
‫Always use the live data by returning data as live data.
‫The room library ensures that the data is observed by the repository or ViewModel allowing for real
‫time updates when the underlying data changes.
‫And if you have any trouble in the live data import from the lifecycle, you can import the dependency
‫for it.
‫By using this live data implement station and the live or the view model here.
‫So I'll copy this implementation for the ViewModel and the live data.
‫In the next video we'll talk about ViewModel so I can import it here.
‫Latest versions of Android X, The life cycle is included.
‫So as you can see, I imported Android X dot lifecycle dot live data without any problem and usage of
‫this dependency.
‫Also the live data and I need to use the lifecycle version variable sync now so I can use ViewModel
‫and the live data in my application.
‫If I have any problem, just add the dependency.
‫If we go to the repository now, I need to change the returning type of this method to live data object.
‫This is the type of the live data and this is the type of the list in the repository.
‫Live data is typically used to expose data from the room database to the ViewModel and ultimately to
‫the UI components, activities or fragments.
‫The repository interacts with the room database to retrieve data and it wraps the data in a live data
‫object before passing it to the ViewModel.
‫By using the live data, the repository ensures that the data can be observed by the ViewModel, and
‫any changes in the underlying data are automatically reflected in the UI without the need to for explicit
‫updates.
‫And one last step in the repository is to change this parameter.
‫Pass an application alt+ enter.
‫And here I need to initialize the context database so contacts database equal to course or contacts
‫database dot get instance.
‫And here I need to pass the context which is the application in this case.
‫And this contact dao this dot contact dao equal to contact database dot get contact Dao.
‫This is how we initialize the database in the repository and how to initialize the Dao in the repository
‫and execute the methods and the background threads.