Sean O
Sean O

Reputation: 236

How to push new data into firedatabase reference and not overwrite it

I have create a class called gameInfo that takes in a time (time takes to finish the game) and a count (game1, game2, game3 etc). In my asveUserInformation() method I take these two bits of information in, and set them to the databasereference. Instead of creating a new unique reference, the reference is overwritten.


private FirebaseAuth mAuth;
    private DatabaseReference myRef;
    private GameInfo gameInfo;
    private String userID;

mAuth = FirebaseAuth.getInstance();
        gameInfo = new GameInfo();
        userID = mAuth.getCurrentUser().getUid();

        myRef = FirebaseDatabase.getInstance().getReference().child("game_times").child(userID).child("mazeGame");

private void saveUserInformation(){

        pauseOffSet = SystemClock.elapsedRealtime() - chronometer.getBase();

        gameInfo.setGame_time(pauseOffSet);
        gameInfo.setGame_count(gameCount);

        myRef.push().setValue(gameInfo);
    }

The expected result is for the user to push data into database for the mazeGame child and create a "game_count: 1" "game_time 23456", then create another new reference "game_count: 2" "game_time 54534". Instead the information is overwritten when the method is called. I thought the push() command generates a unique id when placing data into the database? Is this wrong?

Upvotes: 0

Views: 126

Answers (1)

Shermano
Shermano

Reputation: 1150

You have to explicity create this new id:

String myKey = myRef.child(reference).push().key
FirebaseDatabase.getInstance().getReference().child(myKey).child("game_times").child(userID).child("mazeGame");

Upvotes: 2

Related Questions