2021年4月9日星期五

Android Firebase: Overwrite value instead of create an new entry

I'm struggling with a project I want to continue to develop that I found on GitHub. It is just for self-education purposes, because I want to work more efficient with the firebase. Here is the project and the whole code of it. -> Project

Now to my problem. After you finish the game, the app is creating a new entry in the firebase every time. These lines are in the SingeWordActivity.class and they are creating the output for the firebase:

    DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference(Constants.USER_SCORE);      String id = databaseReference.push().getKey();      User user = new User(id, getSharedPreferences(Constants.USER_PREFERENCE, Context.MODE_PRIVATE).getString(Constants.USER_NICK, getString(R.string.OK)),              numberOfLetters / 5, numberOfLetters, true);      if (id != null) {          databaseReference.child(id).setValue(user);      }  }  

The Constants are just linked in a different class, seeing below:

public class Constants {  public static final int BANNER_SCREEN_TIMEOUT = 3000;  public static final int ANIMATION_DURATION = 1500;  public static final String PREF_FIRST_TIME = "isFirstTime";  public static final String USER_NICK = "userNick";  public static final String USER_PREFERENCE = "userPref";  public static final String USER_SCORE = "score-list";  public static final String IS_FIRST_LOGIN = "isFirst";  public static final String GAME_TYPE = "game_type";  public static final String PARAGRAPH = "paragraph";  public static final String SINGLE_WORD = "single_word";      public static int MILLIS_IN_FUTURE = 60000;  public static final int COUNTDOWN_INTERVAL = 1000;  

}

This is how it looks like in the firebase at the end:

View in the firebase

Is there a way that after the Activity, every value (userScore,singelWord,userLetterCount) in the firebase gets updated and these values are linked with the "userName"?

So it is not possible the have duplicate userNames in the firebase?

Is-state, two entries for the same user

https://stackoverflow.com/questions/67028787/android-firebase-overwrite-value-instead-of-create-an-new-entry April 10, 2021 at 05:58AM

没有评论:

发表评论