1

I have document in Firestore that has an array of string elements that should referenced to IDs of other documents as well .

so my scenario is to get array of saved IDs from my Firestore then create for loop on it and query on Firestore to get each documents that meets that ID .

but my problem is I'm try to put every result of query to new array but i get only first element

here is my fire-store like :

http://prntscr.com/mh4ldy

my code :

 void getCoursesDetails(ArrayList<String> openedCourses, MyCallback2 myCallback2){

    // Get full details of every course
 // openedCourses array  here has size of 5 
for (int i = 0; i < openedCourses.size(); i++) {

    DocumentReference docRef = db.collection("courses").document(openedCourses.get(i));

    docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(DocumentSnapshot documentSnapshot) {

            if (documentSnapshot.exists()) {
                detailedCourses.add(documentSnapshot.toObject(Courses.class));
                myCallback2.onCallback(detailedCourses);

            }
        }
    });
}
// when put myCallback2.onCallback(detailedCourses); here i get array of zero cuz Firestore loads data asynchronously
}

So my Question is : how to make my array of detailedCourses to save every element that got from for loop round from firestore result ?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Long story short - addOnSuccessListener is asynchronous and returns immediately. The loop is going to execute many times rapidly and finish before any of the results are available. It sounds like this is not what you want. – Doug Stevenson Feb 06 '19 at 00:16
  • yup i know , so how best solution i have to do – Hesham Elnemr Feb 06 '19 at 08:52

0 Answers0