Question: I need update and if not exist insert row to ROOM DB.

I make this: `productRepository.updateProducts(productsResponse.getProductItems());`

And:

```
@Override
public void updateProducts(final List<ProductItem> products) {
    new Thread(() -> {
        for (ProductItem item : products) {
            Product product = createProduct(item);
            productDao.insert(product);
        }
    }).start();
}

```

And in DAO:

```
@Insert
void insert(Product products);

```

But I have method 

```
@Update
void update(Product product);

```

And I have some questions:

1. both methods is void. How can I return saved Product or boolean flag or inserted count after insert?
2. if I try call `update` and I have not row will it be inserted?
3. How can I update(if not - insert) row and return count updatet or inserted rows?

Answer: 
1. A method, annotated with `@Insert` can return a `long`. This is the newly generated ID for the inserted row. A method, annotated with `@Update` can return an `int`. This is the number of updated rows.
2. `update` will try to update all your fields using the value of the primary key in a `where` clause. If your entity is not persisted in the database yet, the `update` query will not be able to find a row and will not update anything.
3. You can use `@Insert(onConflict = OnConflictStrategy.REPLACE)`. This will try to insert the entity and, if there is an existing row that has the same ID value, it will delete it and replace it with the entity you are trying to insert. Be aware that, if you are using auto generated IDs, this means that the the resulting row will have a different ID than the original that was replaced. If you want to preserve the ID, then you have to come up with a custom way to do it.
UPDATE.

@Upsert added to Room 2.5.0-alpha03. No need for reinventing the wheel)

Thanks to @Ashwini for great idea.

```
@Dao
interface SendDao {
    @Insert(onConflict = OnConflictStrategy.IGNORE)
    suspend fun insert(model: DataModel): Long

    @Update
    suspend fun update(model: DataModel): Int

    @Transaction
    suspend fun insertOrUpdate(model: DataModel): Long {
        val id = insert(model)
        return if (id==-1L) {
            update(model)
            model.id
        } else {
            id
        }
    }
}

```

So we should call: `database.sendDao().insertOrUpdate(DataModel(...))`