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: 
As `@Danail Alexiev` said `@Insert` can return a `long`. `@Update` can return an `int`.

But for what purpose are you using update? If you just want the whole object to be replaced by the new one then just specify the `OnConflictStrategy`

```
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(Product products);

```

**The result will be:** items that don't exist will be added, items that already exist will be replaced by the new ones.

In the case you need to **update just one param** (like quantity for example) you can do something like this

```
  @Insert(onConflict = OnConflictStrategy.REPLACE)
  void insert(Product products);

  @Query("SELECT * from item WHERE id= :id")
  List<Product> getItemById(int id);

  @Query("UPDATE item SET quantity = quantity + 1 WHERE id = :id")
  void updateQuantity(int id)

  void insertOrUpdate(Product item) {
                List<Product> itemsFromDB = getItemById(item.getId())
                if (itemsFromDB.isEmpty())
                    insert(item)
                else
                    updateQuantity(item.getId())   
            }
        }

```

**The result will be:** Try looking up the item in the DB, if found update a property, if not just insert a new item. So you only need to call one method `insertOrUpdate` from your DAO.
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(...))`