0

I want to show the list through the firestore database.

First, save data to String a

docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if(task.isSuccessful()){
                DocumentSnapshot document = task.getResult();
                if(document.exists()){
                    a = document.getString("gender");
                }
            }

        }
    });

And onComplete method is asynchronous so I add while and Thread.sleep

while(a==null){
        Thread.sleep(1000);
    }
    EventChangeListener();

Here is a EventChangeListener() function code

firestore.collection("Users")
            //.whereEqualTo("mbti","ENTP")
            //.whereEqualTo("region","충청남도")
            .whereEqualTo("gender",a)
            .orderBy("time",Query.Direction.DESCENDING).limit(50)
            .addSnapshotListener(new EventListener<QuerySnapshot>() {
                @Override
                public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
                    if(error != null){

                        if(progressDialog.isShowing()){
                            progressDialog.dismiss();
                        }

                        Log.e("Firestore error",error.getMessage());
                        return;

                    }

                    for (DocumentChange dc : value.getDocumentChanges()){

                        if (dc.getType() == DocumentChange.Type.ADDED){
                            userArrayList.add(dc.getDocument().toObject(User.class));
                        }

                        myAdapter.notifyDataSetChanged();
                        if(progressDialog.isShowing()){
                            progressDialog.dismiss();
                        }

                    }

                }
            });

I want to repeat the while statement every 100 millisecond so EventChangeListener() function can recognize String a.

I wonder if counting time affects performance.

Eye Lamp
  • 69
  • 5
  • 1
    Don't use timers or loops to wait for the result, but instead put the code that needs the data from the database *inside* the `onComplete` handler that gets called when the data is available. See https://stackoverflow.com/questions/48499310/how-to-return-a-documentsnapshot-as-a-result-of-a-method/48500679#48500679 and https://stackoverflow.com/questions/51000169/how-to-check-a-certain-data-already-exists-in-firestore-or-not/51002413#51002413 – Frank van Puffelen Mar 02 '22 at 04:44
  • 1
    There is no way you can do that. Firebase API is asynchronous. So please check the duplicate to see how can you solve this using a callback. You might also be interested in reading this article, [How to read data from Cloud Firestore using get()?](https://medium.com/firebase-tips-tricks/how-to-read-data-from-cloud-firestore-using-get-bf03b6ee4953). Besides that, don't put your main thread to sleep, that's not how you deal with async operations. – Alex Mamo Mar 02 '22 at 07:29

0 Answers0