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
- public abstract class BaseDAO<T> {
- @Insert(onConflict = IGNORE)
- public abstract long insert(T var1);
- @Insert(onConflict = IGNORE)
- @NotNull
- public abstract List insert(@NotNull List<T> var1);
- @Update
- public abstract void update(T var1);
- @Update
- public abstract void update(@NotNull List<T> var1);
- @Transaction
- public void upsert(@Nullable T obj) {
- if (obj != null) {
- long id = this.insert(obj);
- if (id <= -1) {
- this.update(obj);
- }
- }
- }
- @Transaction
- public void upsert(@Nullable List<T> list) {
- if (list != null) {
- List insertedResults = this.insert(list);
- List updateList = new ArrayList();
- for(int i=0; i < insertedResults.size(); i++) {
- if (list.get(i) <= -1) {
- updateList.add(list.get(i));
- }
- }
- if (updateList.size() > 0) {
- this.update(updateList);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment