• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Get rowId after inserted Room Persistence Library

wgeorgio

Lurker
All I have to do is get the return value from the insert as a long. I got that but it isn't working, I am getting back 0 from the returned value. I am using a DAO, Repository and ViewModel as stated in the Google CodeLabs. I have put in multiple logs and they proved useless. I started putting in break points and checking the variable of id of player or also the one I made rowId and still all of the come back 0 in many places.

Player Class
Java:
@Entity(tableName = "player_table")
public class Player {
@PrimaryKey(autoGenerate = true)
private long id;
@NonNull
@ColumnInfo(name = "username")
private String username;
}

DAO

Java:
@Insert
long insert(Player player);
Repository

Java:
public long insert(Player player) {
   new insertAsyncTask(mPlayerDao).execute(player);
   rowId = player.getId();
return rowId;
}
ViewModel

Java:
public long insert(Player player){
   rowId = mRepository.insert(player);
   return rowId;
}
Activity

Java:
String playerString = editTextUsername.getText().toString();
Player player = new Player(playerString);
long rowId = mDreamViewModel.insert(player);
 
re Do the changes in player (Asign the id) inside the AsyncTask. Something like this:
[code = java] la clase privada InsertAsyncPlayer extiende AsyncTask <Player, Void, Void> {
MyDao myDao;
public InsertAsyncPlayer (MyDao dao) {
myDao = dao;
}
@Anular
protected Void doInBackground (Player ... players) {
long idGetted = myDao.insertPlayer (jugadores [0]);
jugadores [0] .setId (idGetted); // aquí es donde se actualiza el objeto del reproductor.
devolver nulo;
}
}[/código]
 
Back
Top Bottom