0

I like to know how to properly integrate the Room Library within an Android Application. Some articles I have seen uses a Singleton approach using a Respository for database calls, while other uses some form of Dependecy Injection (Dagger 2). I like to know the proper integration of this library?

Thanks

ShaidK
  • 65
  • 1
  • 2
  • 4
  • What about consulting the [official documentation](https://developer.android.com/topic/libraries/architecture/adding-components.html). – NiVeR Apr 12 '18 at 15:25

2 Answers2

2

ROOM Database

  • Room is an ORM (Object Relational Mapper) for SQLite database in Android developement and part of Android Jetpack.
  • Provides an abstraction layer over SQLite database
  • Amount of boilerplate code is reduced
  • Compile-time verification of SQL queries

ROOM Database components

  • It has 3 main components Entity, Database, DAO.
  • Entity is data class annotated with @Entity is like creating a table in SQLite and the variables in the model class are like columns in the table.
  • Database is an abstract class which extends RoomDatabase and has list of entities associated with the database.
  • DAO(Data Access Object) is an interface which defines the operations to be performed in our database.

ROOM Database Implementation

  • Step 1 creating Entity data class

    @Entity(tableName = "yourOwnTableName")

    data class LocalData(

      val column1: String?,
    
      val column2: String?,  
    
      @PrimaryKey(autoGenerate = true) val product_local_id: Int?= null
    

    )

  • Step 2 creating Database

    @Database(entities = [LocalData::class], version = 1, exportSchema = false) @TypeConverters

    abstract class LocalDB : RoomDatabase() {

      abstract fun localDataDao() : LocalDataDAO
    
      companion object{
    
          private var instance : LocalDB ? = null
    
          fun getInstance(context: Context) : LocalDB ?{
              if(instance == null){
                  synchronized(LocalDB ::class){
                      instance = Room.databaseBuilder(context.applicationContext, LocalDB::class.java, "localBD.db").allowMainThreadQueries().build()
                  }
              }
              return instance
          }
    
          fun destroyInstance(){
              instance = null
          }
      }
    

    }

  • Step 3 creating DAO

    @Dao interface LocalDAO {

      @Insert
      fun insertData(productEntity: LocalData) : Long
    
      @Delete
      fun deleteData(productEntity: LocalData) : Int
    
      @Query("Select * from yourOwnTableName")
      fun showAllProducts(): List<LocalData>
    
      @Query("SELECT COUNT(*) FROM yourOwnTableName")
      fun totalProducts(): Long
    

    }

  • Step 4 is optional is creating repository

class ProductRepository(context: Context) {

var dbms : LocalDAO = LocalDB.getInstance(context)?.localDataDao()!!

fun insertData(productEntity: LocalData) : Long{
    return dbms.insertData(productEntity)
}

fun deleteData(productEntity: LocalData) : Int{
    return dbms.deleteData(productEntity)
}

fun getAllData() : List<LocalData> {
    return dbms.showAllProducts()
}

fun checkProductExist(id : Int) : Boolean{
    return dbms.exists(id)
}

fun totalProductsInCart() : Long{
    return dbms.totalProducts()
}

}

Rohit S
  • 714
  • 5
  • 7
0

I think the best place is codelabs, it has a simple implementation, as well as a more complex one. All using Room.

Edit: Above links seems to be removed. Here are a couple of resources to replace that:

  1. Android room with a view
  2. Room + LiveData + ViewModel
Suleyman
  • 2,765
  • 2
  • 18
  • 31