Ignore
@Insert(onConflict = OnConflictStrategy.IGNORE)
@Update(onConflict = OnConflictStrategy.IGNORE)
What SQLite Does
This strategy maps to INSERT OR IGNORE
or UPDATE OR IGNORE
statements. This has two key differences to how FAIL
works:
- It does not throw any sort of exception.
- It tries to process everything that the statement should affect. So, if there are 100 rows to be updated, and the 50th row winds up with a constraint violation, that row is skipped, but SQLite continues to try to process any not-yet-updated rows.
The result is that everything that can be inserted or updated is inserted or updated, with individual rows being skipped where they fail on constraint violations.
This is risky, in that you may not necessarily have a good way of knowing that some of your requested data manipulations did not take effect.
Effects in Room
Since IGNORE
does not trigger an exception, Room will commit the transaction that contains your @Insert
or @Update
work. Hence, this works, insofar as Room does not reject changes that SQLite would otherwise accept because Room rolled back the transaction. You still suffer from not knowing what exactly was changed by your @Insert
or @Update
method, though.
However, for SQL statements that you know will only affect one row, IGNORE
is a perfectly reasonable solution.
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.