Guest User

Untitled

a guest
Jul 18th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. import android.arch.persistence.room.*
  2. import android.arch.persistence.room.OnConflictStrategy.*
  3. import kotlin.collections.ArrayList
  4.  
  5. @Dao
  6. abstract class BaseDAO<T> {
  7. @Insert(onConflict = IGNORE)
  8. abstract fun insert(obj: T): Long
  9.  
  10. @Insert(onConflict = IGNORE)
  11. abstract fun insert(list: List<T>): List<Long>
  12.  
  13. @Update
  14. abstract fun update(obj: T)
  15.  
  16. @Update
  17. abstract fun update(obj: List<T>)
  18.  
  19. @Transaction
  20. open fun upsert(obj: T?){
  21. if (obj == null) return
  22. val id = insert(obj)
  23. if (id <= -1){
  24. update(obj)
  25. }
  26. }
  27.  
  28. @Transaction
  29. open fun upsert(list: List<T>?){
  30. if(list == null) return
  31. val insertedResults: List<Long> = insert(list)
  32. val updateList: MutableList<T> = ArrayList()
  33.  
  34. for ((idx, obj) in insertedResults.withIndex()){
  35. if (obj <= -1){
  36. updateList.add(list[idx])
  37. }
  38. }
  39. if (updateList.any()){
  40. update(updateList)
  41. }
  42. }
  43. }
Add Comment
Please, Sign In to add comment