Ad
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?
Ad
Answer
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");
Ad
source: stackoverflow.com
Related Questions
- → should I choose reactjs+f7 or f7+vue.js?
- → Phonegap Android write to sd card
- → Local reference jquery script in nanohttpd (Android)
- → Click to navigate on mobile devices
- → How to allow api access to android or ios app only(laravel)?
- → Access the Camera and CameraRoll on Android using React Native?
- → React native change listening port
- → What is the default unit of style in React Native?
- → Google play market autocomplete icon
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → Using Laravel with Genymotion
- → react native using like web-based ajax function
- → react native pdf View
Ad