‫Welcome back.
‫We created our entity class.
‫Now let's move to the second part which is the Dao.
‫Dao stands for Data Access Object.
‫A Dao is a design pattern that provides an abstract interface to some type of database or other persistence
‫mechanism.
‫It allows you to interact with database by defining methods to perform crude.
‫The create, read, update and delete operations on your data.
‫Move to Android Studio and inside this package I'll create a new Kotlin interface and I'll name it as
‫contact Dao.
‫Click enter.
‫So this interface, the data Access object defines the methods to interact with database room uses these
‫methods to generate the necessary code to perform the database operations.
‫I'll start with the first method.
‫Suspend function insert contact.
‫It's of type contact and returns lock, and I'll annotate this function with add.
‫Insert the insert annotation.
‫This annotation is used by Room Database library to identify that the associated method is used for
‫inserting data into the database.
‫When you annotate a method with add, insert annotation, room uses it to generate the necessary code
‫to insert one or more entities into the corresponding database table.
‫The suspend function or and the suspend keyword here is used to mark the method as a coroutine, so
‫this function insert contact is a coroutine function.
‫Coroutines are used for asynchronous programming in Kotlin.
‫In Android development, coroutines are often used to perform database operations.
‫One of the main thread to prevent blocking the UI.
‫So I'm going to use suspend function in order to execute this function in the background, and not to
‫block and to freeze the UI thread.
‫The returning type is of type long, so when you insert a single entity, room can return the auto generated
‫primary key if applicable, or minus one for the insertion failed.
‫So remember suspend is used for creating coroutines.
‫And coroutines are used for asynchronous programming in Kotlin.
‫Here I'm going to perform the insert contact method in the background thread.
‫In order to prevent the blocking of the main UI thread.
‫The second function will be at update and I'll mark it with update annotation.
‫Suspend function.
‫Update contact took into the parameter a contact object and return nothing.
‫Also, I'm going to create the third function which is for delete suspend function.
‫Delete contact takes into the parameter a contact object.
‫So here I used insert, update and delete annotations to declare methods for inserting, updating and
‫deleting entities in that database.
‫Now I'm going to introduce another annotation which is at query.
‫The query annotation is used for custom queries.
‫You can use placeholders like id to pass parameters to your queries.
‫For example, if I want to delete all the users, not specific users like this and the not specific
‫contact from the database, I can use and create a function delete all and here you can specify the
‫SQL query delete from contacts table.
‫So the SQL query inside the annotation specifies that you want to delete all records from the the contacts
‫table.
‫And again I used the spend in order to avoid blocking the main thread when performing database operations.
‫The last method or the last query I need to create here custom query is to get all users in the database
‫function, not suspend function, because I'm going to select and display all the users and all the
‫records in the database directly.
‫And this is a fast function.
‫No need to use suspend get all contacts in DB, it's returning type is live data.
‫And here I'm going to add list of contacts.
‫And the query should be select all which is this.
‫Risk.
‫From the contacts table, select all from contacts table.
‫This is an SQL select statement that receives and retrieves all columns or columns that presents this
‫asterisk from the contacts table and the query here.
‫Annotation in room database is used to define custom SQL queries for database operations.
‫The method get all contacts in database.
‫Returns a live data of a list of contacts.
‫The live data is a data holder class that can be observed for changes in the context of Android development.
‫Using live data is common when dealing with room database queries, because it allows you to automatically
‫update the UI whenever the underlying data changes.
‫I want from you to focus with me and to review the concepts, the live data, the view model and other
‫advanced topic in the previous videos because we're going to use it here in this application.
‫So Alt Plus enter to import the live data class.