Mos*_*led 3 android transactions firebase google-cloud-firestore
我正在使用 cloud firestore transaction 来编辑 cloud firestore 上的一些数据,但出现此错误:
FirebaseFirestoreException: Every document read in a transaction must also be written.
Run Code Online (Sandbox Code Playgroud)
我已经在firebase的教程中修改了可能导致交易失败的案例,但我没有找到关于这个错误的任何信息。这是我的代码:
if (getCurrentPlayer(player1Uid) == 2) {
Log.v("limitingChallenges", "current player is 2");
DocumentSnapshot snapshotForPlayer2 = transaction.get(player2Reference);
if (snapshotForPlayer2.getLong("totalChallengesNo") != null) {
totalChallengesNo = snapshotForPlayer2.getLong("totalChallengesNo");
}
if (snapshotForPlayer2.getLong("todayChallengesNo") != null) {
todayChallengesNo = snapshotForPlayer2.getLong("todayChallengesNo");
}
if (player1Score == player2Score) {
if (snapshotForPlayer2.getLong("noOfDraws") != null)
noOfDraws = snapshotForPlayer2.getLong("noOfDraws");
newPoints = snapshotForPlayer2.getLong("points") + (long) drawChallengePoints;
newNoOfDraws = noOfDraws + (long) 1;
transaction.update(player2Reference, "points", newPoints);
transaction.update(player2Reference, "totalChallengesNo", totalChallengesNo + 1);
transaction.update(player2Reference, "todayChallengesNo", todayChallengesNo + 1);
if (totalChallengesNo != 0) {
transaction.update(player2Reference, "noOfDraws", newNoOfDraws);
} else {
transaction.update(player2Reference, "noOfDraws", 1);
transaction.update(player2Reference, "noOfWins", 0);
transaction.update(player2Reference, "noOfLoses", 0);
}
return null;
} else if (player2Score > player1Score) {
if (snapshotForPlayer2.getLong("noOfWins") != null)
noOfWins = snapshotForPlayer2.getLong("noOfWins");
newPoints = snapshotForPlayer2.getLong("points") + (long) wonChallengePoints;
newNoOfWins = noOfWins + (long) 1;
transaction.update(player2Reference, "points", newPoints);
transaction.update(player2Reference, "totalChallengesNo", totalChallengesNo + 1);
transaction.update(player2Reference, "todayChallengesNo", todayChallengesNo + 1);
if (totalChallengesNo != 0) {
transaction.update(player2Reference, "noOfWins", newNoOfWins);
} else {
transaction.update(player2Reference, "noOfDraws", 0);
transaction.update(player2Reference, "noOfWins", 1);
transaction.update(player2Reference, "noOfLoses", 0);
}
return null;
} else {
if (snapshotForPlayer2.getLong("noOfLoses") != null)
noOfLoses = snapshotForPlayer2.getLong("noOfLoses");
newNoOfLoses = noOfLoses + (long) 1;
transaction.update(player2Reference, "totalChallengesNo", totalChallengesNo + 1);
transaction.update(player2Reference, "todayChallengesNo", todayChallengesNo + 1);
if (totalChallengesNo != 0) {
transaction.update(player2Reference, "noOfLoses", newNoOfLoses);
} else {
transaction.update(player2Reference, "noOfDraws", 0);
transaction.update(player2Reference, "noOfWins", 0);
transaction.update(player2Reference, "noOfLoses", 1);
}
}
} else if (getCurrentPlayer(player1Uid) == 1) {
DocumentSnapshot snapshotForPlayer1 = transaction.get(player1Reference);
if (snapshotForPlayer1.getLong("todayChallengesNo") != null) {
todayChallengesNo = snapshotForPlayer1.getLong("todayChallengesNo");
}
transaction.update(player2Reference, "totalChallengesNo", totalChallengesNo + 1);
transaction.update(player2Reference, "todayChallengesNo", todayChallengesNo + 1);
Log.v("limitingChallenges", "current player is 1"
+ " , new todayChallengesNo is " + todayChallengesNo + 1);
}
return todayChallengesNo + 1;
}
}).addOnSuccessListener(new OnSuccessListener<Long>() {
@Override
public void onSuccess(Long aLong) {
Log.v("limitingChallenges", "onSuccess , aLong is : " + aLong.toString());
if (aLong > 3) {
Toast.makeText(context, "????? ??? " + (dailyChallengesNumber - aLong) + " ?????? ??? ????? ??? ??? ??????", Toast.LENGTH_SHORT).show();
} else if (aLong == 2) {
Toast.makeText(context, "????? ??? " + " ?????? ??? ????? ??? ??? ??????", Toast.LENGTH_SHORT).show();
} else if (aLong == 1) {
Toast.makeText(context, "????? ??? " + " ???? ???? ??? ????? ??? ??? ??????", Toast.LENGTH_SHORT).show();
} else if (aLong < 1) {
Toast.makeText(context, "?? ????? ??? ?????? ???? ??? ????? ????? ?????? ??? ???? ?????? ?????", Toast.LENGTH_SHORT).show();
}
setSavedTodayChallengesNo(context, aLong);
}
})
Run Code Online (Sandbox Code Playgroud)
我已经多次修改代码,但我找不到问题,错误信息也不够明显,无法准确地确定问题是什么
您正在阅读交易中的两个文件:
DocumentSnapshot snapshotForPlayer1 = transaction.get(player1Reference);
DocumentSnapshot snapshotForPlayer2 = transaction.get(player2Reference);
Run Code Online (Sandbox Code Playgroud)
但是你只会回信给player2Reference. 如果不需要回写player1Reference,则在事务之前而不是在事务内部读取它。
| 归档时间: |
|
| 查看次数: |
1006 次 |
| 最近记录: |