Solution 1
Try insert first. If failed, then perform an update.
@Daointerface PinDao { @Insert(onConflict = OnConflictStrategy.IGNORE) fun insertIgnore(entity: Pin) : Long @Update fun update(entity: Pin) @Transaction fun insertOrUpdate(entity: Pin) { if (insertIgnore(entity) == -1L) { update(entity) } }}
Solution 2
If a row with the same ID already exist, it will delete the existing row and insert a new row. If you are using auto generated ID, this solution probably won't work.
@Insert(onConflict = OnConflictStrategy.REPLACE)fun insertReplace(entity: T) : Long
Solution 3
If you are using auto generated id, you can check if ID is empty and perform insert, else perform update.
If ID is not realiable, you can create an is_new
field to help determine if you should perform insert or update.