【发布时间】:2020-05-14 15:13:45
【问题描述】:
在创建测验应用程序时,当玩家单击某个按钮时,它会在数据库中创建新游戏并打开下一个活动(游戏活动)。我不确定哪种方法是在数据库中设置新文档的最佳方法。
基本上我想知道的是,以下哪种方法是最好的方法,我的假设是否正确。目标是找到确保用户仅在创建所有文档后才能进入下一个活动的最佳方式。
- 方法:
我认为这比第二种方法更快,但有可能无法创建某些文档/子集合。 (我创建了更多文档,但我只展示了一些简单性)
...
gameRef.document(gameId).collection("Uid").document("Uid").set(uidPlayers);
gameRef.document(gameId).collection("Turn").document("Turn").set(mTurn);
gameRef.document(gameId).collection("Game Status").document("Game Status").set(gameStatus);
gameRef.document(gameId).collection("Timestamp").document("Timestamp").set(timeStamp);
Intent i = new Intent(FriendDetailActivity.this, GameActivity.class);
startActivity(i);
...
- 方法:
这种方法比第一种方法更安全,因为它确保在用户退出此活动之前创建每个文档/子集合,但它确实需要更长的时间。我也简化了这里的代码。它更安全,因为在每次创建后我都会添加一个 onCompleteListener。
...
gameRef.document(gameId).collection("Round").document("Round").set(round)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
db.collection("User").document(uidPlayer1).collection("Games").document(uidPlayer2).set(gameUser)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
db.collection("User").document(uidPlayer2).collection("Games").document(uidPlayer1).set(gameUser2)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Intent i = new Intent(FriendDetailActivity.this, GameActivity.class);
startActivity(i);
}
});
}
});
}
});
...
【问题讨论】:
标签: android firebase google-cloud-firestore