Home > Blockchain >  How do I know if firebase firestore has finished reading data?
How do I know if firebase firestore has finished reading data?

Time:04-09

I have this piece of code here that grabs the username based on the ID:

    DocumentReference df = fstore.collection("Users").document(user.getUid());
    df.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if (task.isSuccessful())
            {
                DocumentSnapshot doc = task.getResult();
                if (doc.exists())
                {
                    username.setText("@"  doc.get("Username").toString());
                }
            }
        }
    });

But I noticed that it takes a while to change the username when I run the app. I want to display a progress bar while Firestore is retrieving data and disable it once it's done, how do I know if Firestore has finished grabbing the username?

CodePudding user response:

Try using an inProgress flag. Set the flag to true when you're about to make the call to retrieve the data and set it back to false once you're done. Use this flag to know when to display a progress bar (or a load spinner).

CodePudding user response:

The simplest solution would be to add a ProgressBar in your layout file. Assuming you have a ConstraintLayout, you can add it like this:

<ProgressBar
    android:id="@ id/progressBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:visibility="gone"/>

And then find it in your activity class like this:

ProgressBar progressBar = findViewById(R.layout.progressBar)

And use it in this way:

progressBar.setVisibility(View.VISIBLE) \\           
  • Related