Database handling
with
Room
CC4.0 Kathleen Cole
About me
Sergi Martínez
@sergiandreplace
Android GDE & dev at Schibsted Spain
Disclaimer:
All examples are
in Kotlin
What is Room?
A library to handle android
databases via an abstraction
layer
Tl,dr;
An ORM
Components
Entity
A class representing a
database row
DAO
An interface defining
how to access to db
Database
Holds definition for
DAOs and Entities
Before anything else
repositories {
jcenter()
maven { url 'https://maven.google.com' }
}
def room = '1.0.0-beta2'
implementation "android.arch.persistence.room:runtime:${room}"
kapt "android.arch.persistence.room:compiler:${room}"
Entities
Simple entity
@Entity(tableName = "products")
class ProductEntity(
@PrimaryKey() @ColumnInfo(name = "id") var id: Long,
@ColumnInfo(name = "name") var name: String,
@ColumnInfo(name = "description") var description: String,
@ColumnInfo(name = "qty") var quantity: Long
)
Simple entity
@Entity(tableName = "products")
class ProductEntity(
@PrimaryKey() @ColumnInfo(name = "id") var id: Long,
@ColumnInfo(name = "name") var name: String,
@ColumnInfo(name = "description") var description: String,
@ColumnInfo(name = "qty") var quantity: Long
)
Simple entity
@Entity(tableName = "products")
class ProductEntity(
@PrimaryKey() @ColumnInfo(name = "id") var id: Long,
@ColumnInfo(name = "name") var name: String,
@ColumnInfo(name = "description") var description: String,
@ColumnInfo(name = "qty") var quantity: Long
)
Simple entity
@Entity(tableName = "products")
class ProductEntity(
@PrimaryKey() @ColumnInfo(name = "id") var id: Long,
@ColumnInfo(name = "name") var name: String,
@ColumnInfo(name = "description") var description: String,
@ColumnInfo(name = "qty") var quantity: Long
)
Indexes
@Entity(tableName = "inventory",
indices = arrayOf(Index("provider_id", "product_id", unique = true)))
class InventoryEntity(
@PrimaryKey() var id: Long,
@ColumnInfo(name = "provider_id") var providerId: Long,
@ColumnInfo(name = "product_id") var productId: Long,
@ColumnInfo(name = "qty") var quantity: Long
)
Composed PK
@Entity(tableName = "inventory",
primaryKeys = arrayOf("provider_id", "product_id"))
class InventoryEntity(
@ColumnInfo(name = "provider_id") var providerId: Long,
@ColumnInfo(name = "product_id") var productId: Long,
@ColumnInfo(name = "qty") var quantity: Long
)
Foreign keys
@Entity(
tableName = "inventory",
foreignKeys = arrayOf(
ForeignKey(
entity = ProductEntity::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("product_id")
)
)
)
class InventoryEntity(
@PrimaryKey() var id: Long,
@ColumnInfo(name = "provider_id") var providerId: Long,
@ColumnInfo(name = "product_id") var productId: Long,
@ColumnInfo(name = "qty") var quantity: Long
)
Foreign keys
@Entity(
tableName = "inventory",
foreignKeys = arrayOf(
ForeignKey(
entity = ProductEntity::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("product_id")
)
)
)
class InventoryEntity(
@PrimaryKey() var id: Long,
@ColumnInfo(name = "provider_id") var providerId: Long,
@ColumnInfo(name = "product_id") var productId: Long,
@ColumnInfo(name = "qty") var quantity: Long
)
Foreign keys
@Entity(
tableName = "inventory",
foreignKeys = arrayOf(
ForeignKey(
entity = ProductEntity::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("product_id")
)
)
)
class InventoryEntity(
@PrimaryKey() var id: Long,
@ColumnInfo(name = "provider_id") var providerId: Long,
@ColumnInfo(name = "product_id") var productId: Long,
@ColumnInfo(name = "qty") var quantity: Long
)
Foreign keys
@Entity(
tableName = "inventory",
foreignKeys = arrayOf(
ForeignKey(
entity = ProductEntity::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("product_id")
)
)
)
class InventoryEntity(
@PrimaryKey() var id: Long,
@ColumnInfo(name = "provider_id") var providerId: Long,
@ColumnInfo(name = "product_id") var productId: Long,
@ColumnInfo(name = "qty") var quantity: Long
)
Foreign keys
@Entity(
tableName = "inventory",
foreignKeys = arrayOf(
ForeignKey(
entity = ProductEntity::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("product_id")
)
)
)
class InventoryEntity(
@PrimaryKey() var id: Long,
@ColumnInfo(name = "provider_id") var providerId: Long,
@ColumnInfo(name = "product_id") var productId: Long,
@ColumnInfo(name = "qty") var quantity: Long
)
Foreign keys
@Entity(
tableName = "inventory",
foreignKeys = arrayOf(
ForeignKey(
entity = ProductEntity::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("product_id")
)
)
)
class InventoryEntity(
@PrimaryKey() var id: Long,
@ColumnInfo(name = "provider_id") var providerId: Long,
@ColumnInfo(name = "product_id") var productId: Long,
@ColumnInfo(name = "qty") var quantity: Long
)
Nested objects
@Entity(tableName = "products")
class ProductEntity(
@PrimaryKey() @ColumnInfo(name = "id") var id: Long,
@ColumnInfo(name = "name") var name: String,
@ColumnInfo(name = "description") var description: String,
@Embedded var info: ProductInfo
)
class ProductInfo(
@ColumnInfo(name = "color") var color: String,
@ColumnInfo(name = "size") var size: String
)
products
id name description color size
Nested objects
@Entity(tableName = "products")
class ProductEntity(
@PrimaryKey() @ColumnInfo(name = "id") var id: Long,
@ColumnInfo(name = "name") var name: String,
@ColumnInfo(name = "description") var description: String,
@Embedded var info: ProductInfo
)
class ProductInfo(
@ColumnInfo(name = "color") var color: String,
@ColumnInfo(name = "size") var size: String
)
products
id name description color size
Nested objects
@Entity(tableName = "products")
class ProductEntity(
@PrimaryKey() @ColumnInfo(name = "id") var id: Long,
@ColumnInfo(name = "name") var name: String,
@ColumnInfo(name = "description") var description: String,
@Embedded var info: ProductInfo
)
class ProductInfo(
@ColumnInfo(name = "color") var color: String,
@ColumnInfo(name = "size") var size: String
)
products
id name description color size
Nested objects
@Entity(tableName = "products")
class ProductEntity(
@PrimaryKey() @ColumnInfo(name = "id") var id: Long,
@ColumnInfo(name = "name") var name: String,
@ColumnInfo(name = "description") var description: String,
@Embedded var info: ProductInfo
)
class ProductInfo(
@ColumnInfo(name = "color") var color: String,
@ColumnInfo(name = "size") var size: String
)
products
id name description color size
DAOs
Simple DAO
@Dao
interface ProductsDao {
@Insert(onConflict = REPLACE)
fun insert(productEntity: ProductEntity): Long
@Query("Select * from products")
fun getAll(): List<ProductEntity>
@Update
fun update(productEntity: ProductEntity): Int
@Delete
fun delete(productEntity: ProductEntity): Int
}
Simple DAO
@Dao
interface ProductsDao {
@Insert(onConflict = REPLACE)
fun insert(productEntity: ProductEntity): Long
@Query("Select * from products")
fun getAll(): List<ProductEntity>
@Update
fun update(productEntity: ProductEntity): Int
@Delete
fun delete(productEntity: ProductEntity): Int
}
Simple DAO
@Dao
interface ProductsDao {
@Insert(onConflict = REPLACE)
fun insert(productEntity: ProductEntity): Long
@Query("Select * from products")
fun getAll(): List<ProductEntity>
@Update
fun update(productEntity: ProductEntity): Int
@Delete
fun delete(productEntity: ProductEntity): Int
}
Row id
Simple DAO
@Dao
interface ProductsDao {
@Insert(onConflict = REPLACE)
fun insert(productEntity: ProductEntity): Long
@Query("Select * from products")
fun getAll(): List<ProductEntity>
@Update
fun update(productEntity: ProductEntity): Int
@Delete
fun delete(productEntity: ProductEntity): Int
}
Simple DAO
@Dao
interface ProductsDao {
@Insert(onConflict = REPLACE)
fun insert(productEntity: ProductEntity): Long
@Query("Select * from products")
fun getAll(): List<ProductEntity>
@Update
fun update(productEntity: ProductEntity): Int
@Delete
fun delete(productEntity: ProductEntity): Int
}
# Modified rows
Simple DAO
@Dao
interface ProductsDao {
@Insert(onConflict = REPLACE)
fun insert(productEntity: ProductEntity): Long
@Query("Select * from products")
fun getAll(): List<ProductEntity>
@Update
fun update(productEntity: ProductEntity): Int
@Delete
fun delete(productEntity: ProductEntity): Int
}
Insert
@Dao
interface ProductsDao {
@Insert(onConflict = REPLACE)
fun insert(product: ProductEntity): Long
@Insert(onConflict = REPLACE)
fun insertAll(vararg products: ProductEntity) : List<Long>
@Insert(onConflict = ROLLBACK)
fun insertTwo(product: ProductEntity, otherProduct: ProductEntity): List<Long>
@Insert(onConflict = ABORT)
fun weirdInsert(product: ProductEntity, products: List<ProductEntity>): List<Long>
}
Update
@Dao
interface ProductsDao {
@Update()
fun update(product: ProductEntity): Int
@Update()
fun update(vararg users: ProductEntity): Int
}
Delete
@Dao
interface ProductsDao {
@Delete()
fun delete(product: ProductEntity): Int
@Delete()
fun delete(vararg users: ProductEntity): Int
}
Only primary key is matched
Query
@Dao
interface ProductsDao {
@Query("Select * from products")
fun getAllAsList(): List<ProductEntity>
@Query("SELECT * FROM products")
fun getAllAsArray(): Array<ProductEntity>
@Query("SELECT * FROM products WHERE name = :name")
fun getProductsWithName(name: String): List<ProductEntity>
@Query("SELECT * FROM products WHERE name like :name OR description like :name")
fun searchProduct(term: String): List<ProductEntity>
@Query("SELECT * FROM products WHERE name in (:names)")
fun getProductsByName(names: List<String>): List<ProductEntity>
@Query("SELECT * FROM products WHERE qty >= :minQty AND qty <= :maxQty")
fun getProductsWithinInventory(minQty: Int, maxQty: Int): List<ProductEntity>
}
Query
@Dao
interface ProductsDao {
@Query("Select * from products")
fun getAllAsList(): List<ProductEntity>
@Query("SELECT * FROM products")
fun getAllAsArray(): Array<ProductEntity>
@Query("SELECT * FROM products WHERE name = :name")
fun getProductsWithName(name: String): List<ProductEntity>
@Query("SELECT * FROM products WHERE name like :name OR description like :name")
fun searchProduct(term: String): List<ProductEntity>
@Query("SELECT * FROM products WHERE name in (:names)")
fun getProductsByName(names: List<String>): List<ProductEntity>
@Query("SELECT * FROM products WHERE qty >= :minQty AND qty <= :maxQty")
fun getProductsWithinInventory(minQty: Int, maxQty: Int): List<ProductEntity>
}
Query
@Dao
interface ProductsDao {
@Query("Select * from products")
fun getAllAsList(): List<ProductEntity>
@Query("SELECT * FROM products")
fun getAllAsArray(): Array<ProductEntity>
@Query("SELECT * FROM products WHERE name = :name")
fun getProductsWithName(name: String): List<ProductEntity>
@Query("SELECT * FROM products WHERE name like :name OR description like :name")
fun searchProduct(term: String): List<ProductEntity>
@Query("SELECT * FROM products WHERE name in (:names)")
fun getProductsByName(names: List<String>): List<ProductEntity>
@Query("SELECT * FROM products WHERE qty >= :minQty AND qty <= :maxQty")
fun getProductsWithinInventory(minQty: Int, maxQty: Int): List<ProductEntity>
}
Query
@Dao
interface ProductsDao {
@Query("Select * from products")
fun getAllAsList(): List<ProductEntity>
@Query("SELECT * FROM products")
fun getAllAsArray(): Array<ProductEntity>
@Query("SELECT * FROM products WHERE name = :name")
fun getProductsWithName(name: String): List<ProductEntity>
@Query("SELECT * FROM products WHERE name like :name OR description like :name")
fun searchProduct(term: String): List<ProductEntity>
@Query("SELECT * FROM products WHERE name in (:names)")
fun getProductsByName(names: List<String>): List<ProductEntity>
@Query("SELECT * FROM products WHERE qty >= :minQty AND qty <= :maxQty")
fun getProductsWithinInventory(minQty: Int, maxQty: Int): List<ProductEntity>
}
Query
@Dao
interface ProductsDao {
@Query("Select * from products")
fun getAllAsList(): List<ProductEntity>
@Query("SELECT * FROM products")
fun getAllAsArray(): Array<ProductEntity>
@Query("SELECT * FROM products WHERE name = :name")
fun getProductsWithName(name: String): List<ProductEntity>
@Query("SELECT * FROM products WHERE name like :name OR description like :name")
fun searchProduct(term: String): List<ProductEntity>
@Query("SELECT * FROM products WHERE name in (:names)")
fun getProductsByName(names: List<String>): List<ProductEntity>
@Query("SELECT * FROM products WHERE qty >= :minQty AND qty <= :maxQty")
fun getProductsWithinInventory(minQty: Int, maxQty: Int): List<ProductEntity>
}
Query
@Dao
interface ProductsDao {
@Query("Select * from products")
fun getAllAsList(): List<ProductEntity>
@Query("SELECT * FROM products")
fun getAllAsArray(): Array<ProductEntity>
@Query("SELECT * FROM products WHERE name = :name")
fun getProductsWithName(name: String): List<ProductEntity>
@Query("SELECT * FROM products WHERE name like :name OR description like :name")
fun searchProduct(term: String): List<ProductEntity>
@Query("SELECT * FROM products WHERE name in (:names)")
fun getProductsByName(names: List<String>): List<ProductEntity>
@Query("SELECT * FROM products WHERE qty >= :minQty AND qty <= :maxQty")
fun getProductsWithinInventory(minQty: Int, maxQty: Int): List<ProductEntity>
}
Query
@Dao
interface ProductsDao {
@Query("Select * from products")
fun getAllAsList(): List<ProductEntity>
@Query("SELECT * FROM products")
fun getAllAsArray(): Array<ProductEntity>
@Query("SELECT * FROM products WHERE name = :name")
fun getProductsWithName(name: String): List<ProductEntity>
@Query("SELECT * FROM products WHERE name like :name OR description like :name")
fun searchProduct(term: String): List<ProductEntity>
@Query("SELECT * FROM products WHERE name in (:names)")
fun getProductsByName(names: List<String>): List<ProductEntity>
@Query("SELECT * FROM products WHERE qty >= :minQty AND qty <= :maxQty")
fun getProductsWithinInventory(minQty: Int, maxQty: Int): List<ProductEntity>
}
Query
@Dao
interface ProductsDao {
@Query("SELECT * FROM products WHERE id = :id")
fun getProducts(id: Long): ProductEntity
@Query("SELECT COUNT(*) FROM products")
fun getProductsCount(): Int
@Query("SELECT COUNT(*) AS count, name FROM products GROUP BY name")
fun getProductsCountByName(): List<ProductCountEntity>
}
class ProductCountEntity(
@ColumnInfo(name = "name") var name: String,
@ColumnInfo(name = "count") var count: Int
)
Query
@Dao
interface ProductsDao {
@Query("SELECT * FROM products WHERE id = :id")
fun getProducts(id: Long): ProductEntity
@Query("SELECT COUNT(*) FROM products")
fun getProductsCount(): Int
@Query("SELECT COUNT(*) AS count, name FROM products GROUP BY name")
fun getProductsCountByName(): List<ProductCountEntity>
}
class ProductCountEntity(
@ColumnInfo(name = "name") var name: String,
@ColumnInfo(name = "count") var count: Int
)
Query
@Dao
interface ProductsDao {
@Query("SELECT * FROM products WHERE id = :id")
fun getProducts(id: Long): ProductEntity
@Query("SELECT COUNT(*) FROM products")
fun getProductsCount(): Int
@Query("SELECT COUNT(*) AS count, name FROM products GROUP BY name")
fun getProductsCountByName(): List<ProductCountEntity>
}
class ProductCountEntity(
@ColumnInfo(name = "name") var name: String,
@ColumnInfo(name = "count") var count: Int
)
Query
@Dao
interface ProductsDao {
@Query("SELECT * FROM products WHERE id = :id")
fun getProducts(id: Long): ProductEntity
@Query("SELECT COUNT(*) FROM products")
fun getProductsCount(): Int
@Query("SELECT COUNT(*) AS count, name FROM products GROUP BY name")
fun getProductsCountByName(): List<ProductCountEntity>
}
class ProductCountEntity(
@ColumnInfo(name = "name") var name: String,
@ColumnInfo(name = "count") var count: Int
)
Database
Database
@Database(
entities = arrayOf(
ProductEntity::class,
InventoryEntity::class
),
version = 1
)
abstract class MyDatabase : RoomDatabase() {
abstract fun productsDao(): ProductsDao
abstract fun inventoryDao(): InventoryDao
}
Database
@Database(
entities = arrayOf(
ProductEntity::class,
InventoryEntity::class
),
version = 1
)
abstract class MyDatabase : RoomDatabase() {
abstract fun productsDao(): ProductsDao
abstract fun inventoryDao(): InventoryDao
}
Database
@Database(
entities = arrayOf(
ProductEntity::class,
InventoryEntity::class
),
version = 1
)
abstract class MyDatabase : RoomDatabase() {
abstract fun productsDao(): ProductsDao
abstract fun inventoryDao(): InventoryDao
}
Database
@Database(
entities = arrayOf(
ProductEntity::class,
InventoryEntity::class
),
version = 1
)
abstract class MyDatabase : RoomDatabase() {
abstract fun productsDao(): ProductsDao
abstract fun inventoryDao(): InventoryDao
}
Using database
val database: MyDatabase = Room
.databaseBuilder(applicationContext, MyDatabase::class.java, "database")
.build()
val products = database.productsDao().getAllAsList()
Using database
val database: MyDatabase = Room
.databaseBuilder(applicationContext, MyDatabase::class.java, "database")
.build()
val products = database.productsDao().getAllAsList()
Using database
val database: MyDatabase = Room
.databaseBuilder(applicationContext, MyDatabase::class.java, "database")
.build()
val products = database.productsDao().getAllAsList()
Use a single instance of
database!
Type Converters
Date (Boo)
vs.
Three-ten-abp (Yaay!)
Type converters
class DateTimeConverter {
private val df : DateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME
@TypeConverter
fun toTimestamp(dateTime: LocalDateTime) : String {
return dateTime.format(df)
}
@TypeConverter
fun toDateTime(timestamp:String) : LocalDateTime {
return LocalDateTime.parse(timestamp, df);
}
}
Type converters
class DateTimeConverter {
private val df : DateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME
@TypeConverter
fun toTimestamp(dateTime: LocalDateTime) : String {
return dateTime.format(df)
}
@TypeConverter
fun toDateTime(timestamp:String) : LocalDateTime {
return LocalDateTime.parse(timestamp, df);
}
}
Type converters
@Database(entities = arrayOf(ProductEntity::class), version = 1)
@TypeConverters(DateTimeConverter::class)
abstract class MyDatabase : RoomDatabase() {
abstract fun productsDao(): ProductsDao
}
Type converters
@Dao
@TypeConverters(DateTimeConverter::class)
interface InventoryDao {
@Query("Select * from inventory where date = :date")
fun getByDate(date: LocalDateTime)
@Query("Select * from inventory where date = :date")
fun getByDate2(date: LocalDateTime)
}
Type converters
@Dao
interface InventoryDao {
@Query("Select * from inventory where date = :date")
@TypeConverters(DateTimeConverter::class)
fun getByDate(date: LocalDateTime)
@Query("Select * from inventory where date = :date")
fun getByDate2(date: LocalDateTime)
}
Type converters
@Dao
interface InventoryDao {
@Query("Select * from inventory where date = :date")
fun getByDate(date: LocalDateTime)
@Query("Select * from inventory where date = :date")
fun getByDate2(@TypeConverters(DateTimeConverter::class) date: LocalDateTime)
}
Type converters
@Entity(tableName = "products")
class ProductEntity(
@PrimaryKey() @ColumnInfo(name = "id") var id: Long,
@ColumnInfo(name = "name") var name: String,
@ColumnInfo(name = "description") var description: String,
@ColumnInfo(name = "qty") var quantity: Long,
@ColumnInfo(name = "purchase_date") var purchaseDate: LocalDateTime
)
Type converters
@Entity(tableName = "products")
@TypeConverters(DateTimeConverter::class)
class ProductEntity(
@PrimaryKey() @ColumnInfo(name = "id") var id: Long,
@ColumnInfo(name = "name") var name: String,
@ColumnInfo(name = "description") var description: String,
@ColumnInfo(name = "qty") var quantity: Long,
@ColumnInfo(name = "purchase_date") var purchaseDate: LocalDateTime
)
Type converters
@Entity(tableName = "products")
class ProductEntity(
@PrimaryKey() @ColumnInfo(name = "id") var id: Long,
@ColumnInfo(name = "name") var name: String,
@ColumnInfo(name = "description") var description: String,
@ColumnInfo(name = "qty") var quantity: Long,
@TypeConverters(DateTimeConverter::class)
@ColumnInfo(name = "purchase_date") var purchaseDate: LocalDateTime
)
Migrations
val migrationFrom1To2 = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE products ADD COLUMN prices INTEGER")
}
}
val migrationFrom2To3 = object : Migration(2, 3) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE products ADD COLUMN color TEXT")
}
}
val database: MyDatabase = Room
.databaseBuilder(applicationContext, MyDatabase::class.java, "database")
.addMigrations(migrationFrom1To2, migrationFrom2To3)
.build()
Migrations
val migrationFrom1To2 = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE products ADD COLUMN prices INTEGER")
}
}
val migrationFrom2To3 = object : Migration(2, 3) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE products ADD COLUMN color TEXT")
}
}
val database: MyDatabase = Room
.databaseBuilder(applicationContext, MyDatabase::class.java, "database")
.addMigrations(migrationFrom1To2, migrationFrom2To3)
.build()
Migrations
val migrationFrom1To2 = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE products ADD COLUMN prices INTEGER")
}
}
val migrationFrom2To3 = object : Migration(2, 3) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE products ADD COLUMN color TEXT")
}
}
val database: MyDatabase = Room
.databaseBuilder(applicationContext, MyDatabase::class.java, "database")
.addMigrations(migrationFrom1To2, migrationFrom2To3)
.build()
Migrations
val migrationFrom1To2 = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE products ADD COLUMN prices INTEGER")
}
}
val migrationFrom2To3 = object : Migration(2, 3) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE products ADD COLUMN color TEXT")
}
}
val database: MyDatabase = Room
.databaseBuilder(applicationContext, MyDatabase::class.java, "database")
.addMigrations(migrationFrom1To2, migrationFrom2To3)
.build()
Migrations
@Database(entities = arrayOf(ProductEntity::class), version = 3)
@TypeConverters(DateTimeConverter::class)
abstract class MyDatabase : RoomDatabase() {
abstract fun productsDao(): ProductsDao
}
Warning!
If no migration is provided,
Room will rebuilt database
based on schema
LiveData
@Query("Select * from products")
fun getAllAsList(): LiveData<List<ProductEntity>>
RxJava 2
def room = '1.0.0-beta2'
implementation "android.arch.persistence.room:runtime:${room}"
kapt "android.arch.persistence.room:compiler:${room}"
implementation "android.arch.persistence.room:rxjava2:${room}"
RxJava 2
@Query("Select * from products")
fun getAllAsList(): Flowable<List<ProductEntity>>
@Query("SELECT * FROM products WHERE id = :id")
fun getProductById(id: Long): Single<ProductEntity>
@Query("SELECT * FROM products WHERE name = :name")
fun getProductByName(name: String): Maybe<ProductEntity>
RxJava 2
Flowable Maybe Single
No data - Just onComplete onError
Data onNext onSuccess onSuccess
Update onNext - -
Moar things!
But enough for today
Questions?
Database handling with room

Database handling with room

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
    A library tohandle android databases via an abstraction layer
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
    Before anything else repositories{ jcenter() maven { url 'https://maven.google.com' } } def room = '1.0.0-beta2' implementation "android.arch.persistence.room:runtime:${room}" kapt "android.arch.persistence.room:compiler:${room}"
  • 12.
  • 13.
    Simple entity @Entity(tableName ="products") class ProductEntity( @PrimaryKey() @ColumnInfo(name = "id") var id: Long, @ColumnInfo(name = "name") var name: String, @ColumnInfo(name = "description") var description: String, @ColumnInfo(name = "qty") var quantity: Long )
  • 14.
    Simple entity @Entity(tableName ="products") class ProductEntity( @PrimaryKey() @ColumnInfo(name = "id") var id: Long, @ColumnInfo(name = "name") var name: String, @ColumnInfo(name = "description") var description: String, @ColumnInfo(name = "qty") var quantity: Long )
  • 15.
    Simple entity @Entity(tableName ="products") class ProductEntity( @PrimaryKey() @ColumnInfo(name = "id") var id: Long, @ColumnInfo(name = "name") var name: String, @ColumnInfo(name = "description") var description: String, @ColumnInfo(name = "qty") var quantity: Long )
  • 16.
    Simple entity @Entity(tableName ="products") class ProductEntity( @PrimaryKey() @ColumnInfo(name = "id") var id: Long, @ColumnInfo(name = "name") var name: String, @ColumnInfo(name = "description") var description: String, @ColumnInfo(name = "qty") var quantity: Long )
  • 17.
    Indexes @Entity(tableName = "inventory", indices= arrayOf(Index("provider_id", "product_id", unique = true))) class InventoryEntity( @PrimaryKey() var id: Long, @ColumnInfo(name = "provider_id") var providerId: Long, @ColumnInfo(name = "product_id") var productId: Long, @ColumnInfo(name = "qty") var quantity: Long )
  • 18.
    Composed PK @Entity(tableName ="inventory", primaryKeys = arrayOf("provider_id", "product_id")) class InventoryEntity( @ColumnInfo(name = "provider_id") var providerId: Long, @ColumnInfo(name = "product_id") var productId: Long, @ColumnInfo(name = "qty") var quantity: Long )
  • 19.
    Foreign keys @Entity( tableName ="inventory", foreignKeys = arrayOf( ForeignKey( entity = ProductEntity::class, parentColumns = arrayOf("id"), childColumns = arrayOf("product_id") ) ) ) class InventoryEntity( @PrimaryKey() var id: Long, @ColumnInfo(name = "provider_id") var providerId: Long, @ColumnInfo(name = "product_id") var productId: Long, @ColumnInfo(name = "qty") var quantity: Long )
  • 20.
    Foreign keys @Entity( tableName ="inventory", foreignKeys = arrayOf( ForeignKey( entity = ProductEntity::class, parentColumns = arrayOf("id"), childColumns = arrayOf("product_id") ) ) ) class InventoryEntity( @PrimaryKey() var id: Long, @ColumnInfo(name = "provider_id") var providerId: Long, @ColumnInfo(name = "product_id") var productId: Long, @ColumnInfo(name = "qty") var quantity: Long )
  • 21.
    Foreign keys @Entity( tableName ="inventory", foreignKeys = arrayOf( ForeignKey( entity = ProductEntity::class, parentColumns = arrayOf("id"), childColumns = arrayOf("product_id") ) ) ) class InventoryEntity( @PrimaryKey() var id: Long, @ColumnInfo(name = "provider_id") var providerId: Long, @ColumnInfo(name = "product_id") var productId: Long, @ColumnInfo(name = "qty") var quantity: Long )
  • 22.
    Foreign keys @Entity( tableName ="inventory", foreignKeys = arrayOf( ForeignKey( entity = ProductEntity::class, parentColumns = arrayOf("id"), childColumns = arrayOf("product_id") ) ) ) class InventoryEntity( @PrimaryKey() var id: Long, @ColumnInfo(name = "provider_id") var providerId: Long, @ColumnInfo(name = "product_id") var productId: Long, @ColumnInfo(name = "qty") var quantity: Long )
  • 23.
    Foreign keys @Entity( tableName ="inventory", foreignKeys = arrayOf( ForeignKey( entity = ProductEntity::class, parentColumns = arrayOf("id"), childColumns = arrayOf("product_id") ) ) ) class InventoryEntity( @PrimaryKey() var id: Long, @ColumnInfo(name = "provider_id") var providerId: Long, @ColumnInfo(name = "product_id") var productId: Long, @ColumnInfo(name = "qty") var quantity: Long )
  • 24.
    Foreign keys @Entity( tableName ="inventory", foreignKeys = arrayOf( ForeignKey( entity = ProductEntity::class, parentColumns = arrayOf("id"), childColumns = arrayOf("product_id") ) ) ) class InventoryEntity( @PrimaryKey() var id: Long, @ColumnInfo(name = "provider_id") var providerId: Long, @ColumnInfo(name = "product_id") var productId: Long, @ColumnInfo(name = "qty") var quantity: Long )
  • 25.
    Nested objects @Entity(tableName ="products") class ProductEntity( @PrimaryKey() @ColumnInfo(name = "id") var id: Long, @ColumnInfo(name = "name") var name: String, @ColumnInfo(name = "description") var description: String, @Embedded var info: ProductInfo ) class ProductInfo( @ColumnInfo(name = "color") var color: String, @ColumnInfo(name = "size") var size: String ) products id name description color size
  • 26.
    Nested objects @Entity(tableName ="products") class ProductEntity( @PrimaryKey() @ColumnInfo(name = "id") var id: Long, @ColumnInfo(name = "name") var name: String, @ColumnInfo(name = "description") var description: String, @Embedded var info: ProductInfo ) class ProductInfo( @ColumnInfo(name = "color") var color: String, @ColumnInfo(name = "size") var size: String ) products id name description color size
  • 27.
    Nested objects @Entity(tableName ="products") class ProductEntity( @PrimaryKey() @ColumnInfo(name = "id") var id: Long, @ColumnInfo(name = "name") var name: String, @ColumnInfo(name = "description") var description: String, @Embedded var info: ProductInfo ) class ProductInfo( @ColumnInfo(name = "color") var color: String, @ColumnInfo(name = "size") var size: String ) products id name description color size
  • 28.
    Nested objects @Entity(tableName ="products") class ProductEntity( @PrimaryKey() @ColumnInfo(name = "id") var id: Long, @ColumnInfo(name = "name") var name: String, @ColumnInfo(name = "description") var description: String, @Embedded var info: ProductInfo ) class ProductInfo( @ColumnInfo(name = "color") var color: String, @ColumnInfo(name = "size") var size: String ) products id name description color size
  • 29.
  • 30.
    Simple DAO @Dao interface ProductsDao{ @Insert(onConflict = REPLACE) fun insert(productEntity: ProductEntity): Long @Query("Select * from products") fun getAll(): List<ProductEntity> @Update fun update(productEntity: ProductEntity): Int @Delete fun delete(productEntity: ProductEntity): Int }
  • 31.
    Simple DAO @Dao interface ProductsDao{ @Insert(onConflict = REPLACE) fun insert(productEntity: ProductEntity): Long @Query("Select * from products") fun getAll(): List<ProductEntity> @Update fun update(productEntity: ProductEntity): Int @Delete fun delete(productEntity: ProductEntity): Int }
  • 32.
    Simple DAO @Dao interface ProductsDao{ @Insert(onConflict = REPLACE) fun insert(productEntity: ProductEntity): Long @Query("Select * from products") fun getAll(): List<ProductEntity> @Update fun update(productEntity: ProductEntity): Int @Delete fun delete(productEntity: ProductEntity): Int } Row id
  • 33.
    Simple DAO @Dao interface ProductsDao{ @Insert(onConflict = REPLACE) fun insert(productEntity: ProductEntity): Long @Query("Select * from products") fun getAll(): List<ProductEntity> @Update fun update(productEntity: ProductEntity): Int @Delete fun delete(productEntity: ProductEntity): Int }
  • 34.
    Simple DAO @Dao interface ProductsDao{ @Insert(onConflict = REPLACE) fun insert(productEntity: ProductEntity): Long @Query("Select * from products") fun getAll(): List<ProductEntity> @Update fun update(productEntity: ProductEntity): Int @Delete fun delete(productEntity: ProductEntity): Int } # Modified rows
  • 35.
    Simple DAO @Dao interface ProductsDao{ @Insert(onConflict = REPLACE) fun insert(productEntity: ProductEntity): Long @Query("Select * from products") fun getAll(): List<ProductEntity> @Update fun update(productEntity: ProductEntity): Int @Delete fun delete(productEntity: ProductEntity): Int }
  • 36.
    Insert @Dao interface ProductsDao { @Insert(onConflict= REPLACE) fun insert(product: ProductEntity): Long @Insert(onConflict = REPLACE) fun insertAll(vararg products: ProductEntity) : List<Long> @Insert(onConflict = ROLLBACK) fun insertTwo(product: ProductEntity, otherProduct: ProductEntity): List<Long> @Insert(onConflict = ABORT) fun weirdInsert(product: ProductEntity, products: List<ProductEntity>): List<Long> }
  • 37.
    Update @Dao interface ProductsDao { @Update() funupdate(product: ProductEntity): Int @Update() fun update(vararg users: ProductEntity): Int }
  • 38.
    Delete @Dao interface ProductsDao { @Delete() fundelete(product: ProductEntity): Int @Delete() fun delete(vararg users: ProductEntity): Int } Only primary key is matched
  • 39.
    Query @Dao interface ProductsDao { @Query("Select* from products") fun getAllAsList(): List<ProductEntity> @Query("SELECT * FROM products") fun getAllAsArray(): Array<ProductEntity> @Query("SELECT * FROM products WHERE name = :name") fun getProductsWithName(name: String): List<ProductEntity> @Query("SELECT * FROM products WHERE name like :name OR description like :name") fun searchProduct(term: String): List<ProductEntity> @Query("SELECT * FROM products WHERE name in (:names)") fun getProductsByName(names: List<String>): List<ProductEntity> @Query("SELECT * FROM products WHERE qty >= :minQty AND qty <= :maxQty") fun getProductsWithinInventory(minQty: Int, maxQty: Int): List<ProductEntity> }
  • 40.
    Query @Dao interface ProductsDao { @Query("Select* from products") fun getAllAsList(): List<ProductEntity> @Query("SELECT * FROM products") fun getAllAsArray(): Array<ProductEntity> @Query("SELECT * FROM products WHERE name = :name") fun getProductsWithName(name: String): List<ProductEntity> @Query("SELECT * FROM products WHERE name like :name OR description like :name") fun searchProduct(term: String): List<ProductEntity> @Query("SELECT * FROM products WHERE name in (:names)") fun getProductsByName(names: List<String>): List<ProductEntity> @Query("SELECT * FROM products WHERE qty >= :minQty AND qty <= :maxQty") fun getProductsWithinInventory(minQty: Int, maxQty: Int): List<ProductEntity> }
  • 41.
    Query @Dao interface ProductsDao { @Query("Select* from products") fun getAllAsList(): List<ProductEntity> @Query("SELECT * FROM products") fun getAllAsArray(): Array<ProductEntity> @Query("SELECT * FROM products WHERE name = :name") fun getProductsWithName(name: String): List<ProductEntity> @Query("SELECT * FROM products WHERE name like :name OR description like :name") fun searchProduct(term: String): List<ProductEntity> @Query("SELECT * FROM products WHERE name in (:names)") fun getProductsByName(names: List<String>): List<ProductEntity> @Query("SELECT * FROM products WHERE qty >= :minQty AND qty <= :maxQty") fun getProductsWithinInventory(minQty: Int, maxQty: Int): List<ProductEntity> }
  • 42.
    Query @Dao interface ProductsDao { @Query("Select* from products") fun getAllAsList(): List<ProductEntity> @Query("SELECT * FROM products") fun getAllAsArray(): Array<ProductEntity> @Query("SELECT * FROM products WHERE name = :name") fun getProductsWithName(name: String): List<ProductEntity> @Query("SELECT * FROM products WHERE name like :name OR description like :name") fun searchProduct(term: String): List<ProductEntity> @Query("SELECT * FROM products WHERE name in (:names)") fun getProductsByName(names: List<String>): List<ProductEntity> @Query("SELECT * FROM products WHERE qty >= :minQty AND qty <= :maxQty") fun getProductsWithinInventory(minQty: Int, maxQty: Int): List<ProductEntity> }
  • 43.
    Query @Dao interface ProductsDao { @Query("Select* from products") fun getAllAsList(): List<ProductEntity> @Query("SELECT * FROM products") fun getAllAsArray(): Array<ProductEntity> @Query("SELECT * FROM products WHERE name = :name") fun getProductsWithName(name: String): List<ProductEntity> @Query("SELECT * FROM products WHERE name like :name OR description like :name") fun searchProduct(term: String): List<ProductEntity> @Query("SELECT * FROM products WHERE name in (:names)") fun getProductsByName(names: List<String>): List<ProductEntity> @Query("SELECT * FROM products WHERE qty >= :minQty AND qty <= :maxQty") fun getProductsWithinInventory(minQty: Int, maxQty: Int): List<ProductEntity> }
  • 44.
    Query @Dao interface ProductsDao { @Query("Select* from products") fun getAllAsList(): List<ProductEntity> @Query("SELECT * FROM products") fun getAllAsArray(): Array<ProductEntity> @Query("SELECT * FROM products WHERE name = :name") fun getProductsWithName(name: String): List<ProductEntity> @Query("SELECT * FROM products WHERE name like :name OR description like :name") fun searchProduct(term: String): List<ProductEntity> @Query("SELECT * FROM products WHERE name in (:names)") fun getProductsByName(names: List<String>): List<ProductEntity> @Query("SELECT * FROM products WHERE qty >= :minQty AND qty <= :maxQty") fun getProductsWithinInventory(minQty: Int, maxQty: Int): List<ProductEntity> }
  • 45.
    Query @Dao interface ProductsDao { @Query("Select* from products") fun getAllAsList(): List<ProductEntity> @Query("SELECT * FROM products") fun getAllAsArray(): Array<ProductEntity> @Query("SELECT * FROM products WHERE name = :name") fun getProductsWithName(name: String): List<ProductEntity> @Query("SELECT * FROM products WHERE name like :name OR description like :name") fun searchProduct(term: String): List<ProductEntity> @Query("SELECT * FROM products WHERE name in (:names)") fun getProductsByName(names: List<String>): List<ProductEntity> @Query("SELECT * FROM products WHERE qty >= :minQty AND qty <= :maxQty") fun getProductsWithinInventory(minQty: Int, maxQty: Int): List<ProductEntity> }
  • 46.
    Query @Dao interface ProductsDao { @Query("SELECT* FROM products WHERE id = :id") fun getProducts(id: Long): ProductEntity @Query("SELECT COUNT(*) FROM products") fun getProductsCount(): Int @Query("SELECT COUNT(*) AS count, name FROM products GROUP BY name") fun getProductsCountByName(): List<ProductCountEntity> } class ProductCountEntity( @ColumnInfo(name = "name") var name: String, @ColumnInfo(name = "count") var count: Int )
  • 47.
    Query @Dao interface ProductsDao { @Query("SELECT* FROM products WHERE id = :id") fun getProducts(id: Long): ProductEntity @Query("SELECT COUNT(*) FROM products") fun getProductsCount(): Int @Query("SELECT COUNT(*) AS count, name FROM products GROUP BY name") fun getProductsCountByName(): List<ProductCountEntity> } class ProductCountEntity( @ColumnInfo(name = "name") var name: String, @ColumnInfo(name = "count") var count: Int )
  • 48.
    Query @Dao interface ProductsDao { @Query("SELECT* FROM products WHERE id = :id") fun getProducts(id: Long): ProductEntity @Query("SELECT COUNT(*) FROM products") fun getProductsCount(): Int @Query("SELECT COUNT(*) AS count, name FROM products GROUP BY name") fun getProductsCountByName(): List<ProductCountEntity> } class ProductCountEntity( @ColumnInfo(name = "name") var name: String, @ColumnInfo(name = "count") var count: Int )
  • 49.
    Query @Dao interface ProductsDao { @Query("SELECT* FROM products WHERE id = :id") fun getProducts(id: Long): ProductEntity @Query("SELECT COUNT(*) FROM products") fun getProductsCount(): Int @Query("SELECT COUNT(*) AS count, name FROM products GROUP BY name") fun getProductsCountByName(): List<ProductCountEntity> } class ProductCountEntity( @ColumnInfo(name = "name") var name: String, @ColumnInfo(name = "count") var count: Int )
  • 50.
  • 51.
    Database @Database( entities = arrayOf( ProductEntity::class, InventoryEntity::class ), version= 1 ) abstract class MyDatabase : RoomDatabase() { abstract fun productsDao(): ProductsDao abstract fun inventoryDao(): InventoryDao }
  • 52.
    Database @Database( entities = arrayOf( ProductEntity::class, InventoryEntity::class ), version= 1 ) abstract class MyDatabase : RoomDatabase() { abstract fun productsDao(): ProductsDao abstract fun inventoryDao(): InventoryDao }
  • 53.
    Database @Database( entities = arrayOf( ProductEntity::class, InventoryEntity::class ), version= 1 ) abstract class MyDatabase : RoomDatabase() { abstract fun productsDao(): ProductsDao abstract fun inventoryDao(): InventoryDao }
  • 54.
    Database @Database( entities = arrayOf( ProductEntity::class, InventoryEntity::class ), version= 1 ) abstract class MyDatabase : RoomDatabase() { abstract fun productsDao(): ProductsDao abstract fun inventoryDao(): InventoryDao }
  • 55.
    Using database val database:MyDatabase = Room .databaseBuilder(applicationContext, MyDatabase::class.java, "database") .build() val products = database.productsDao().getAllAsList()
  • 56.
    Using database val database:MyDatabase = Room .databaseBuilder(applicationContext, MyDatabase::class.java, "database") .build() val products = database.productsDao().getAllAsList()
  • 57.
    Using database val database:MyDatabase = Room .databaseBuilder(applicationContext, MyDatabase::class.java, "database") .build() val products = database.productsDao().getAllAsList()
  • 58.
    Use a singleinstance of database!
  • 59.
  • 60.
  • 61.
    Type converters class DateTimeConverter{ private val df : DateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME @TypeConverter fun toTimestamp(dateTime: LocalDateTime) : String { return dateTime.format(df) } @TypeConverter fun toDateTime(timestamp:String) : LocalDateTime { return LocalDateTime.parse(timestamp, df); } }
  • 62.
    Type converters class DateTimeConverter{ private val df : DateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME @TypeConverter fun toTimestamp(dateTime: LocalDateTime) : String { return dateTime.format(df) } @TypeConverter fun toDateTime(timestamp:String) : LocalDateTime { return LocalDateTime.parse(timestamp, df); } }
  • 63.
    Type converters @Database(entities =arrayOf(ProductEntity::class), version = 1) @TypeConverters(DateTimeConverter::class) abstract class MyDatabase : RoomDatabase() { abstract fun productsDao(): ProductsDao }
  • 64.
    Type converters @Dao @TypeConverters(DateTimeConverter::class) interface InventoryDao{ @Query("Select * from inventory where date = :date") fun getByDate(date: LocalDateTime) @Query("Select * from inventory where date = :date") fun getByDate2(date: LocalDateTime) }
  • 65.
    Type converters @Dao interface InventoryDao{ @Query("Select * from inventory where date = :date") @TypeConverters(DateTimeConverter::class) fun getByDate(date: LocalDateTime) @Query("Select * from inventory where date = :date") fun getByDate2(date: LocalDateTime) }
  • 66.
    Type converters @Dao interface InventoryDao{ @Query("Select * from inventory where date = :date") fun getByDate(date: LocalDateTime) @Query("Select * from inventory where date = :date") fun getByDate2(@TypeConverters(DateTimeConverter::class) date: LocalDateTime) }
  • 67.
    Type converters @Entity(tableName ="products") class ProductEntity( @PrimaryKey() @ColumnInfo(name = "id") var id: Long, @ColumnInfo(name = "name") var name: String, @ColumnInfo(name = "description") var description: String, @ColumnInfo(name = "qty") var quantity: Long, @ColumnInfo(name = "purchase_date") var purchaseDate: LocalDateTime )
  • 68.
    Type converters @Entity(tableName ="products") @TypeConverters(DateTimeConverter::class) class ProductEntity( @PrimaryKey() @ColumnInfo(name = "id") var id: Long, @ColumnInfo(name = "name") var name: String, @ColumnInfo(name = "description") var description: String, @ColumnInfo(name = "qty") var quantity: Long, @ColumnInfo(name = "purchase_date") var purchaseDate: LocalDateTime )
  • 69.
    Type converters @Entity(tableName ="products") class ProductEntity( @PrimaryKey() @ColumnInfo(name = "id") var id: Long, @ColumnInfo(name = "name") var name: String, @ColumnInfo(name = "description") var description: String, @ColumnInfo(name = "qty") var quantity: Long, @TypeConverters(DateTimeConverter::class) @ColumnInfo(name = "purchase_date") var purchaseDate: LocalDateTime )
  • 70.
    Migrations val migrationFrom1To2 =object : Migration(1, 2) { override fun migrate(database: SupportSQLiteDatabase) { database.execSQL("ALTER TABLE products ADD COLUMN prices INTEGER") } } val migrationFrom2To3 = object : Migration(2, 3) { override fun migrate(database: SupportSQLiteDatabase) { database.execSQL("ALTER TABLE products ADD COLUMN color TEXT") } } val database: MyDatabase = Room .databaseBuilder(applicationContext, MyDatabase::class.java, "database") .addMigrations(migrationFrom1To2, migrationFrom2To3) .build()
  • 71.
    Migrations val migrationFrom1To2 =object : Migration(1, 2) { override fun migrate(database: SupportSQLiteDatabase) { database.execSQL("ALTER TABLE products ADD COLUMN prices INTEGER") } } val migrationFrom2To3 = object : Migration(2, 3) { override fun migrate(database: SupportSQLiteDatabase) { database.execSQL("ALTER TABLE products ADD COLUMN color TEXT") } } val database: MyDatabase = Room .databaseBuilder(applicationContext, MyDatabase::class.java, "database") .addMigrations(migrationFrom1To2, migrationFrom2To3) .build()
  • 72.
    Migrations val migrationFrom1To2 =object : Migration(1, 2) { override fun migrate(database: SupportSQLiteDatabase) { database.execSQL("ALTER TABLE products ADD COLUMN prices INTEGER") } } val migrationFrom2To3 = object : Migration(2, 3) { override fun migrate(database: SupportSQLiteDatabase) { database.execSQL("ALTER TABLE products ADD COLUMN color TEXT") } } val database: MyDatabase = Room .databaseBuilder(applicationContext, MyDatabase::class.java, "database") .addMigrations(migrationFrom1To2, migrationFrom2To3) .build()
  • 73.
    Migrations val migrationFrom1To2 =object : Migration(1, 2) { override fun migrate(database: SupportSQLiteDatabase) { database.execSQL("ALTER TABLE products ADD COLUMN prices INTEGER") } } val migrationFrom2To3 = object : Migration(2, 3) { override fun migrate(database: SupportSQLiteDatabase) { database.execSQL("ALTER TABLE products ADD COLUMN color TEXT") } } val database: MyDatabase = Room .databaseBuilder(applicationContext, MyDatabase::class.java, "database") .addMigrations(migrationFrom1To2, migrationFrom2To3) .build()
  • 74.
    Migrations @Database(entities = arrayOf(ProductEntity::class),version = 3) @TypeConverters(DateTimeConverter::class) abstract class MyDatabase : RoomDatabase() { abstract fun productsDao(): ProductsDao }
  • 75.
    Warning! If no migrationis provided, Room will rebuilt database based on schema
  • 76.
    LiveData @Query("Select * fromproducts") fun getAllAsList(): LiveData<List<ProductEntity>>
  • 77.
    RxJava 2 def room= '1.0.0-beta2' implementation "android.arch.persistence.room:runtime:${room}" kapt "android.arch.persistence.room:compiler:${room}" implementation "android.arch.persistence.room:rxjava2:${room}"
  • 78.
    RxJava 2 @Query("Select *from products") fun getAllAsList(): Flowable<List<ProductEntity>> @Query("SELECT * FROM products WHERE id = :id") fun getProductById(id: Long): Single<ProductEntity> @Query("SELECT * FROM products WHERE name = :name") fun getProductByName(name: String): Maybe<ProductEntity>
  • 79.
    RxJava 2 Flowable MaybeSingle No data - Just onComplete onError Data onNext onSuccess onSuccess Update onNext - -
  • 80.
  • 81.
  • 82.