Guest User

Untitled

a guest
Jul 18th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 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. public abstract class BaseDAO<T> {
  7. @Insert(onConflict = IGNORE)
  8. public abstract long insert(T var1);
  9.  
  10. @Insert(onConflict = IGNORE)
  11. @NotNull
  12. public abstract List insert(@NotNull List<T> var1);
  13.  
  14. @Update
  15. public abstract void update(T var1);
  16.  
  17. @Update
  18. public abstract void update(@NotNull List<T> var1);
  19.  
  20. @Transaction
  21. public void upsert(@Nullable T obj) {
  22. if (obj != null) {
  23. long id = this.insert(obj);
  24. if (id <= -1) {
  25. this.update(obj);
  26. }
  27. }
  28. }
  29.  
  30. @Transaction
  31. public void upsert(@Nullable List<T> list) {
  32. if (list != null) {
  33. List insertedResults = this.insert(list);
  34. List updateList = new ArrayList();
  35.  
  36. for(int i=0; i < insertedResults.size(); i++) {
  37. if (list.get(i) <= -1) {
  38. updateList.add(list.get(i));
  39. }
  40. }
  41.  
  42. if (updateList.size() > 0) {
  43. this.update(updateList);
  44. }
  45. }
  46. }
  47. }
Add Comment
Please, Sign In to add comment