Android Room Batch Insert Or Update (Kotlin)

April 13, 2018

Use varargs with @Insert or @Insertannotation.

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

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

    fun update(entity: Item)

    fun updateAll(vararg entity: Item)    

Insert multiple objects

// insert multiple items
itemDao.insertAll(item1, item2, item3)

Convert List to varargs

// insert list of items
val items = listOf<Item>(item1, item2, item3)

Convert Array to varargs

// insert array of items
val items = arrayOf(item1, item2, item3)
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.