gpt4 book ai didi

android - FirebaseFirestoreException : Every document read in a transaction must also be written

转载 作者:行者123 更新时间:2023-12-04 02:51:54 26 4
gpt4 key购买 nike

我正在使用 cloud firestore 事务编辑 cloud firestore 上的一些数据,但出现此错误:

FirebaseFirestoreException: Every document read in a transaction must also be written.

我已经在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);
}
})

我已经修改了不止一次的代码,但我找不到问题所在,错误信息也不够明显,无法确定到底是什么问题

最佳答案

您正在阅读交易中的两个文档:

DocumentSnapshot snapshotForPlayer1 = transaction.get(player1Reference);
DocumentSnapshot snapshotForPlayer2 = transaction.get(player2Reference);

但是你只写回player2Reference。如果您不需要写回player1Reference,那么在交易之前而不是在交易内部读取它。

关于android - FirebaseFirestoreException : Every document read in a transaction must also be written,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54706169/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com