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: 
You can check your database if there is already item with specific field, for exmaple:

```
@Query("SELECT id FROM items WHERE id = :id LIMIT 1")
fun getItemId(id: String): String?

@Insert
fun insert(item: Item): Long

@Update(onConflict = OnConflictStrategy.REPLACE)
fun update(item: Item): Int

```

Item is Your object, and in your code:

```
 fun insertOrUpdate(item: Item) {
            database.runInTransaction {
                val id = getItemDao().getItemId(item.id)

                if(id == null)
                    getItemDao().insert(item)
                else
                    getItemDao().update(item)
            }
        }

```
You can do with @Upsert

```
@Upsert(entity = Show::class)
fun updateOrInsert(shows: List<ShowUpdate>)

```

with

```
def room_version = "2.5.0-beta01"
implementation "androidx.room:room-runtime:$room_version"
implementation "androidx.room:room-ktx:$room_version"

```

This updates only some fields of Show. Does not update field "isFavorite"

```
@Entity
data class Show(
@PrimaryKey var id: Int = 0,
val title: String? = null,
val subtitle: String? = null,
val imageUrl: String? = null,
val description: String? = null,
val isFromNetwork: Boolean = false,
val isFavorite: Boolean? = false,
val timestamp: Long = System.currentTimeMillis()
)

@Entity
data class ShowUpdate(
@PrimaryKey var id: Int = 0,
val title: String? = null,
val subtitle: String? = null,
val imageUrl: String? = null,
val description: String? = null,
val isFromNetwork: Boolean = false,
val timestamp: Long = System.currentTimeMillis()
)

```