‫The second step in creating our database is to follow the singleton pattern, to provide one instance
‫during the life cycle of the app.
‫A database instance can be resource intensive and creating multiple instances of the database unnecessarily
‫consumes memory and processing power.
‫A singleton ensures that only one instance of the database exists throughout the application's life
‫cycle optimizing resource usage.
‫So for that I'll create a private static an instance from this contact database and I'll call it as
‫instance DB instance.
‫I want from you to focus with me.
‫I'll implement a singleton design pattern in Java to provide one single instance during the life cycle
‫of the app to optimize the resource usage and prevent creating multiple instances of the database.
‫Public static synchronized contact database and get instance.
‫This method is used to provide a contact database when it's cold.
‫I'll pass context as a parameter for this method and let's start writing the code here.
‫I need to check if there is any existing instance of the database during the life cycle of the app,
‫then I'll provide it.
‫Otherwise I'll create a new instance of the room database.
‫So if instance this DB instance equals to null, so there is no instance of the database.
‫I need to create a new room instance.
‫So db instance equal to room dot database builder and inside this builder method I need to pass the
‫context dot, get application context as the first parameter.
‫Then I need to pass the second parameter which is of type class.
‫And here I need to specify the room database and this contact database is acting as the room database,
‫so I need to pass it dot class.
‫And the third parameter is the name of this database and I'll name it as contacts underscore DB again,
‫room dot database builder.
‫This is a factory method provided by the room library to create a new database or access an existing
‫one.
‫It allows you to configure and build a room database instance Context Dot get application Context.
‫Context is typically the context of the application component, like an activity or application.
‫And here get application context method is used to obtain the application context, which is often preferred
‫when working with databases because it has a longer life cycle than, for example, an activity context.
‫This helps prevent memory leaks and this is the database room databases that you've defined using the
‫rooms annotation and this is the name of the database room databases are typically file based, and
‫this name is used to identify the database file on the device's storage.
‫Then I need to call fall back to destructive migration.
‫This method is used to specify a database migration strategy.
‫In this case, this method is used.
‫It means that if a new version of the database schema is detected due to changes in the entity structure
‫room will drop and recreate the database effectively discarding all existing data.
‫This is called destructive migration and is useful during development, or when it's acceptable to lose
‫existing data for production apps.
‫You typically implement more sophisticated migration strategies to prevent data during or to preserve
‫data during schema changes.
‫And finally, the build method is called to build the room database instance according to the specified
‫configuration.
‫This is if there is no instance if DB instance equal to null.
‫Otherwise I need to return the instance that it is created.
‫So if there is any instance created in the previous executions or commands, then I need to return this.
‫DB So this is how we create only one instance of our database to preserve the memory and to prevent
‫it from memory leaks and synchronized is used to prevent any cleaning of the contact database.
‫And this is for the threads.
‫So the get instance method ensures that only.
‫One instance of the contact database is created and used throughout the application's life cycle.
‫This singleton pattern helps maintain consistency, thread, safety and efficient resource utilization
‫when working with a room database.