Android Room Database Transaction

July 24, 2018
val db: AppDatabase = Room.databaseBuilder(app, AppDatabase::class.java, "MyDb.db")
        .build()

db.runInTransaction {
    item.updateImage(imageDataSource)
    dataSource.update(item)
}

or

interface BaseDao<T> {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(entity: T) : Long

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertAll(vararg entity: T)

    @Update
    fun update(entity: T)

    @Update
    fun updateAll(vararg entity: T)

    @Delete
    fun delete(entity: T)
}

@Dao
interface PinDao : BaseDao<Pin> {
    @Transaction
    public void replacePin(newPin: Pin, oldPin: Pin) {
         insert(newPin)
         delete(oldPin)
    }
}
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.