0

I have the following code for an activity of my app. It works by redirecting from another activity. But even though it's run once, it does hundreds of readings. Where is the problem?

public class CalculatedResults extends AppCompatActivity {

    private Intent intent;
    private float clearTurkıshCorrect, clearSocialStudiesCorrect, clearBasicMathCorrect, clearScienceCorrect;
    private TextView txtClearTurkishCorrect,txtClearSocialStudiesCorrect, txtClearBasicMathCorrect,txtClearScienceCorrect, txtRawScore, txtPlacementScore, txtRawRanking, txtPlacementRanking;
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    private static final String TAG = "DocSnippets";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculated_results);

        
        setDataFromFirebase();
    }
 

    private void setDataFromFirebase() {
        DocumentReference docRef = db.collection("rawRankings").document(String.valueOf((int)intent.getDoubleExtra("rawScore",0)));
        docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {
                    DocumentSnapshot document = task.getResult();
                    if (document.exists()) {
                        Log.d(TAG, "DocumentSnapshot data: " + document.getData());
                        txtRawRanking.setText(String.valueOf(document.getData().get("tyt")));
                    } else {
                        Log.d(TAG, "No such document");
                    }
                } else {
                    Log.d(TAG, "get failed with ", task.getException());
                }
            }
        });
    }

    
}

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

0

Posting the comments of @VenkateshTalacheeru, @FrankvanPuffelen as a Community wiki answer.

Firebase Firestore read counts are calculated even you load data in your Firebase console. It will read all the collections and documents you have every time you refresh or real-time syncs. It's expected behavior.

As was mentioned in another thread,

use of the Firebase console will incur reads. If you leave the console open on a collection/document with busy write activity, the console will automatically read the changes that update the console's display. This is very often the source of unexpected reads.

Farid Shumbar
  • 1,360
  • 3
  • 10