Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.arch.persistence.room.*
- import android.arch.persistence.room.OnConflictStrategy.*
- import kotlin.collections.ArrayList
- @Dao
- abstract class BaseDAO<T> {
- @Insert(onConflict = IGNORE)
- abstract fun insert(obj: T): Long
- @Insert(onConflict = IGNORE)
- abstract fun insert(list: List<T>): List<Long>
- @Update
- abstract fun update(obj: T)
- @Update
- abstract fun update(obj: List<T>)
- @Transaction
- open fun upsert(obj: T?){
- if (obj == null) return
- val id = insert(obj)
- if (id <= -1){
- update(obj)
- }
- }
- @Transaction
- open fun upsert(list: List<T>?){
- if(list == null) return
- val insertedResults: List<Long> = insert(list)
- val updateList: MutableList<T> = ArrayList()
- for ((idx, obj) in insertedResults.withIndex()){
- if (obj <= -1){
- updateList.add(list[idx])
- }
- }
- if (updateList.any()){
- update(updateList)
- }
- }
- }
Add Comment
Please, Sign In to add comment