‫Welcome back.
‫Let's continue with this database.
‫And I'm going to follow the singleton design pattern to ensure that only one single database instance
‫of this class will be available in the application.
‫Creating and opening a database connection can be a resource intensive operation.
‫By ensuring that only one instance of the database exists.
‫Singleton pattern.
‫Following this pattern, you avoid unnecessary overhead associated with repeated database creation,
‫so a singleton ensures that there is only one instance of the database leading to a consistent state
‫of the database.
‫This is important for data integrity and preventing unexpected behavior due to multiple instances of
‫the database, so the singleton pattern helps in preventing memory leaks by ensuring that there is only
‫one reference to the database instance.
‫This is particularly relevant in Android, where memory management is crucial.
‫I'll start with companion object.
‫A companion object is a way to define a static members of a class.
‫In this case, it's used to create a singleton instance of the user or the contact database.
‫Then inside it I'm going to create private var instance.
‫It's of type contact database referring to this class and here checking for its null value.
‫This is a nullable private variable that holds a single instance of the contact database.
‫It's initially set to null and I'll mark it as a volatile.
‫The volatile annotation is used to mark the instance variable to ensure that read and writes to this
‫are atomic.
‫This helps to prevent any possible race conditions that may occur during multithreaded operations.
‫So if you have multithreading operations in this application, this volatile annotation will prevent
‫the race condition among this multithreaded operations.
‫Then I'll use function get instance took into the parameter a context object alt plus enter to import
‫it and returns a contact database object.
‫So I'm creating this function and inside it I'll mark it as synchronized and pass this as a parameter.
‫Then why I use the synchronized.
‫This function get instance is marked as synchronized, meaning that only one thread can access it at
‫a time.
‫It's important to avoid potential issues that may arise when multiple threads try to access or modify
‫the instance variable concurrently, so this prevent any multiple access to this instance and modifications
‫for the instance inside this function synchronized function I'll create a var instance equals to instance.
‫I'm associating it and try.
‫If this instance equals to null, then go and create a new instance.
‫Here I'm creating a new database object.
‫I'll start with instance equals to room dot database builder.
‫And here I need to specify the parameters I'll pass context dot application context.
‫The second parameter is contact database class Java.
‫And here I'll name the database contacts underscore db.
‫Then I'll call build function room provides a set of utilities for working with databases.
‫Database builder is a method provided by room to create a database instance or a room database builder
‫object.
‫It takes three parameters.
‫The first one is the context.
‫The application context is.
‫It's generally used to obtain the application context to avoid memory leaks.
‫Contact database class the class reference to that database.
‫This indicates the room database class that you want to create.
‫And here I'm referring to the contact database the contacts underscore db.
‫This is the name of the database.
‫This is the name of the SQLite database file that will be created on the device, and the build method
‫is called on the room database builder to finalize the.
‫Creation of the database instance.
‫This method completes the configuration and returns an instance of the contact database or the database
‫class specified.
‫Putting all together, this code creates a new instance of the contact database by configuring and building
‫it using room's database builder.
‫Then we need to continue.
‫If it is equal to null, then go and create a new database if it exists.
‫And after the creation of this instance, I need to assign it this instance variable and return this
‫instance.
‫Okay.
‫Here how we create our database in room.
‫And we ensure that only one instance of this database will be available during the runtime.