Firebase ID count - Android

Asked

Viewed 264 times

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;
    }
}

1 answer

0


Caique why don’t you do the following creates a variable in the countercounterID firabase that when you create the game you will la and arrow this variable, and in the next changes you can use. Onchange from firebase to change this value.

Now if you want to do this for each user recommend you put this ID within the user registration and each game it starts you change the value there.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.