FirebaseFirestoreException:在事务中读取的每个文档也必须写入

时间:2019-02-15 09:25:20

标签: android firebase transactions google-cloud-firestore

我正在使用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);
        }
    })

我已经多次修改了代码,但是我找不到问题,并且错误消息也不明显,无法确切地确定问题出在何处

1 个答案:

答案 0 :(得分:1)

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

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

但是您只能写回player2Reference。如果您不需要写回player1Reference,请在交易之前而不是交易内部进行读取。