Android Room Batch Insert Or Update (Kotlin)

Apr 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)     @Update    fun update(entity: Item)    @Update    fun updateAll(vararg entity: Item)    }

Insert multiple objects

// insert multiple itemsitemDao.insertAll(item1, item2, item3)

Convert List to varargs

// insert list of itemsval items = listOf<Item>(item1, item2, item3)itemDao.insertAll(*items.toTypedArray())

Convert Array to varargs

// insert array of itemsval items = arrayOf(item1, item2, item3)itemDao.insertAll(*items)

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.