0
In my application, I have an entity called Game which has, among other attributes, a ID. I need that each time this entity is instantiated the ID is incremented + 1. This works until the application is terminated, because when I open the app again the ID count is restarted, that way when I create a new one Game and submit to Firebase, it overwrites the element that already existed with that ID instead of creating a new one. I need to somehow save this Ids, or recover the size of the list of Games already registered in Firebase, so that when the application is started again the ID continue telling where you left off. What’s the best way to do this? And how to do it?
Here’s how I’m doing to count the ID in the Entity Game:
public class Game {
private static int cont = 1;
private int id;
...
//Construtor
public Game() {
this.id = cont++;
}
}
And here’s where I’m submitting the new Game for Firebase in Registergamesactivity2:
private boolean registerGame(Game game){
try{
firebase = FirebaseConfig.getFirebase().child("Game");
firebase.child(String.valueOf(game.getId())).setValue(game);
Toast.makeText(RegisterGamesActivity2.this, "Jogo cadastrado com sucesso!", Toast.LENGTH_LONG).show();
return true;
}
catch (Exception e){
e.printStackTrace();
return false;
}
}