重新启动应用程序时,firebase替换了我的数据

时间:2018-09-04 14:44:07

标签: android firebase firebase-realtime-database

每次用户登录并单击“开始游戏”按钮,我的应用程序都会生成一个gameKey。变量“ gameKey”由以下代码生成,然后将数据保存在gameKey下。问题是,每当我重新启动应用程序时,Firebase都会用生成的新gameKey替换整个数据树。

我想做的是在我的应用每次运行时生成一个新的gameKey而不覆盖旧数据。如果有人能指出我的问题,将非常高兴,非常感谢!

class Mixin: pass

class Y: pass

class X(Mixin, Y): pass

print(X.mro())
# [<class '__main__.X'>, <class '__main__.Mixin'>, <class '__main__.Y'>, <class 'object'>]

这是您建议后的代码:

    final FirebaseDatabase database = FirebaseDatabase.getInstance();
    UserId = mAuth.getCurrentUser().getUid();
    DatabaseReference currentUserId = database.getReference("user").child(UserId);

    gameKey = currentUserId.child("gameinfo").push().getKey();

    Map<String, Object> game = new HashMap<>();
    game.put(gameKey, new Game(tv_player1name.getText().toString(), tv_player2name.getText().toString(), tv_player3name.getText().toString(), tv_playerMename.getText().toString(), gameMode, gameDate ));
    currentUserId.child("gameInfo").updateChildren(game);

Firebase Data:
  - user
      - 5xGKRXeHgThQy70lduPEp3mosTj1   (UID)
            - gameInfo
                  -LLV0H0ZJwYT5M42Obfb
                       gameDate: "20180903_232015"      
                       gameType: "HKMJ"
                       player1name: "peter"
                       player2name: "jenny"
                       player3name: "john"
                       player4name: "wilson"

     *This is the gameKey generated: "LLV0H0ZJwYT5M42Obfb"

此外,我尝试替换此行

  final FirebaseDatabase database = FirebaseDatabase.getInstance();
  DatabaseReference currentUserId = database.getReference("user").child(UserId);
  currentUserId.setValue(new User(mAuth.getCurrentUser().getEmail(), UserPic));
  gameKey = currentUserId.child("gameinfo").push().getKey();

  Map<String, Object> game = new HashMap<>();

  game.put(gameKey, new Game(tv_player1name.getText().toString(), tv_player2name.getText().toString(), tv_player3name.getText().toString(), tv_playerMename.getText().toString(), gameMode, gameDate )); 

  currentUserId.child("gameInfo").child(gameKey).updateChildren(game);

与此行,结果相同

 game.put(gameKey, new Game(tv_player1name.getText().toString(), tv_player2name.getText().toString(), tv_player3name.getText().toString(), tv_playerMename.getText().toString(), gameMode, gameDate )); 

非常感谢您的帮助!

Firebase ScreenShot

1 个答案:

答案 0 :(得分:0)

要解决此问题,请更改以下代码行:

currentUserId.child("gameInfo").updateChildren(game);

currentUserId.child("gameInfo").child(gameKey).updateChildren(game);

基本上,您正在生成一个随机密钥,但根本没有使用它。因此,您需要将gameKey传递给child()方法,如我上面的代码所示。

编辑:要解决整个更新过程,请使用以下代码行:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference gameInfoRef = rootRef.child("user").child(uid).child("gameInfo");
String key = gameInfoRef.push().getKey();
Map<String, Object> game = new HashMap<>();
game.put("player1name", tv_player1name.getText().toString());
game.put("player2name", tv_player2name.getText().toString());
game.put("player3name", tv_player3name.getText().toString());
game.put("playerMename", tv_playerMename.getText().toString());
game.put("gameMode", gameMode);
game.put("gameDate", gameDate);
gameInfoRef.child(key).updateChildren(game);