lincollincol
lincollincol

Reputation: 862

Android room database, replace element by id

How to replace existed object by id in the room? For example, I have User with id 7 and name John. Then I change name to Bob. How to update or replace it by id in the room?

Upvotes: 0

Views: 1293

Answers (2)

pawegio
pawegio

Reputation: 1732

You can use REPLACE strategy on conflict and declare insert method this way:

@Dao
interface UserDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertUser(user: User)
}

Upvotes: 0

Wasim Abuzaher
Wasim Abuzaher

Reputation: 814

Use the update annotation in your Dao, it would update based on the primary key (id in your case)

@Update()
void updateUser(User user);

Upvotes: 2

Related Questions