Mostafa Khaled
Mostafa Khaled

Reputation: 372

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

I am using cloud firestore transaction to edit some data on the cloud firestore but I got this error :

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

I have revised the cases that can make the transaction fails on the tutorial of firebase but I didn't find anything about this error. this is my code :

          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);
        }
    })

I have revised the code more than one time but I can't find the problem and the error message isn't obvious enough to determine what is the problem exctly

Upvotes: 3

Views: 1021

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317372

You are reading two documents in the transaction:

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

But you only ever write back to player2Reference. If you don't need to write back to player1Reference, then read it before the transaction rather than inside the transaction.

Upvotes: 5

Related Questions