‫Hello, my friends.
‫We've created the Entity and the Dao parts of their room database.
‫In this video, we will create the database itself.
‫This is the third part of the room database.
‫So we will create a new abstract class called Node Database that will act as singleton instance for
‫our room database.
‫It will connect with the Dao and start performing the operations with Singleton pattern.
‫Back to Android studio.
‫Inside the database package, I will create a new Kotlin class and here I will name it as node data
‫base.
‫Click Enter.
‫There we go.
‫This is our database.
‫And I will.
‫Created as an abstract class.
‫I don't need to make instances of data or no database class, so we make it as an abstract, and I will
‫annotate this class as a database annotation in order to to tell Android studio that this will act as
‫a room database.
‫So I need to pass here the entities as an array.
‫If you if you have more entities and more tables than the note entity class, you should mention it
‫here.
‫So mention here all the.
‫Entity classes.
‫Alt+ enter to import the package for the.
‫Note.
‫And here you specify the virgin equal to one.
‫Okay, Now, this class, as I lectured before, it will extend from the room database.
‫And it should be an abstract.
‫Now I want to create an abstract function called Get the note the a O, and it will return.
‫Note the.
‫Okay.
‫Here.
‫I need to use a companion object.
‫Companion object is used.
‫To design a singleton pattern, returning only one instance from this object, from this class, from
‫this database during the runtime.
‫So we can't.
‫This is similar to getting only one instance following the singleton pattern.
‫Only one instance during the runtime from the database.
‫And it's a very important in order not to make many instances of the database and prevent many instances
‫of the database and memory leaks and time and so on.
‫So.
‫And by the way, I've lectured about volatile and the companion objects in the previous application.
‫So please go back in order to get more.
‫Uh, notes.
‫Instance.
‫Note database.
‫To null.
‫And here.
‫I'm creating a private.
‫Val lock, which is equal to any.
‫Now I will create here.
‫Operator.
‫Function.
‫Invoke.
‫And context as first parameter and context equal to.
‫Instance.
‫Is it equal to two synchronized?
‫Don't worry.
‫I will lecture everything now.
‫But follow me now.
‫Instance.
‫And create database.
‫This is a function that I will create now.
‫Dot also.
‫And instance, equal it.
‫What I've done here.
‫First of all, the volatile.
‫This annotation Will marks the JVM Java virtual machine backing field of the annotated property as volatile.
‫This is the for the example in this case the instance meaning that writes to this field or are immediately
‫made visible to other threads.
‫So any change to this instance, any change, any rewrite to this instance would be immediately made
‫visible to other threads because we're going to work with a companion object which is a singleton object
‫in Kotlin using any.
‫Any is a function that it is added as an extension to iterable and map interfaces which take.
‫A higher order function as parameter to predicate the condition and return boolean as true if any of
‫the items in the list set or map confirms that condition.
‫Otherwise it will return false.
‫So we've used this Annie to store as log, or you can use it directly here and synchronized in order
‫to see executes the given function block while holding the monitor of the given object lock synchronized.
‫A keyword is used when we deal with multithreading and in order to prevent any unsynchronized data between
‫the objects of the and between the threads, we use the synchronized keyword in order to ensure that
‫we have only one instance of this database called node database.
‫Okay, now I will create the create database function.
‫Here.
‫Font.
‫Private function.
‫Create database.
‫It took me the parameter the context as parameter equal to room database or room dot database builder.
‫And here I specify the context as the first parameter.
‫Application context.
‫The second parameter is the node database, which is the class that I'm working in class dot Java.
‫The third parameter is the name of our database node underscore DB.
‫Dot build.
‫Don't worry about these things.
‫We will clarify them all when dealing with the Multithreading and the Coroutines.
‫But I'm the good news that I'm going to tell you that these are like template codes.
‫You should use them when you create database.
‫And this is a very, very good example and a template that you will go and use it when dealing with
‫the databases.
‫So don't worry, you can copy and paste all these codes, but you change the database name here and
‫the entities only whenever you should use the room database in your applications.
‫So don't worry, just copy and paste these codes in the node database.