Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Get a Room - Android Architecture Component

Get a Room - Android Architecture Component

This presentation talks about room which is one of the new android architecture component that Google introduce in the mid of Google I/O 2017.
The Room persistence library provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite.

KMKLabs

March 21, 2018
Tweet

More Decks by KMKLabs

Other Decks in Programming

Transcript

  1. • Object mapping library for persisting data with SQLite From

    the documentation : “Room provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite.” What is Room ?
  2. Why Room ? ➔ Compile-time verification of SQL queries ➔

    Less boilerplate code ➔ High degree of testability ➔ Full integration with other android architecture ➔ Support RxJava
  3. @Entity Represents a table within the database. User.kt data class

    User ( val uid: Int; val name: String; val age: Int; val relationship: String; ) @Entity(tableName= “user”) data class User ( @PrimaryKey val uid: Int; @ColumnInfo(name=”fullname”) val name: String; val age: Int; @Ignore val relationship: String; )
  4. @Dao Contains the methods used for accessing the database. Annotation

    for Dao @Insert @Update @Delete @Query UserDAO.kt @Dao interface UserDao { @Insert() fun insertUser(user: User) @Update(onConflict = REPLACE) fun insertUser(user: User) @Query("SELECT * FROM user) fun getAllUser(): List<User> }
  5. Database Contains the database holder and serves as the main

    access point for the underlying connection to your app's persisted, relational data. MyDatabase.kt @Database(entities = {User.class}, version = 1) abstract class MyDatabase: RoomDatabase() { abstract userDao(): UserDao } myDatabase = Room.databaseBuilder(context, MyDatabase::class.java, DB_NAME).build() myDatabase = Room.inMemoryDatabaseBuilder(context, MyDatabase::class.java).build()
  6. How to use it ? ➔ Add dependencies ➔ Create

    Entity ➔ Create DAO ➔ Create Database ➔ Test it
  7. Step #1 - add dependencies Add the Google Maven repository

    - build.gradle(project) maven { url 'https://maven.google.com' } Room - build.gradle(app) compile "android.arch.persistence.room:runtime:1.0.0" kapt "android.arch.persistence.room:compiler:1.0.0" Room Testing support - build.gradle(app) androidTestCompile "android.arch.persistence.room:testing:1.0.0" Room RxJava support (optional) - build.gradle(app) compile "android.arch.persistence.room:rxjava2:1.0.0"