title: Firestore task with for each loop
body: <p>I'm trying to retrieve document references from Firestore in a for loop (without iterating through the subset). I need to wait for the loop to finish, wait for data to be received and on success, submit this data to the Firestore. Currently, my method does not wait for data to be received what so ever since it is async.</p>
  
  <p>It would probably be a good idea to create a method that returns a Task and then wait for a result. Suggestions?</p>
  
  <pre><code>ArrayList&lt;String&gt; documentPath = new ArrayList&lt;&gt;();
       private void getDocumentRef() {
          try {
              for (String path : documentPath) {
                  db.document(path).get().addOnCompleteListener(new OnCompleteListener&lt;DocumentSnapshot&gt;() {
                      @Override
                      public void onComplete(@NonNull Task&lt;DocumentSnapshot&gt; task) {
  
                          if (task.isSuccessful() &amp;&amp; task.getResult() != null) {
                              if (task.getResult().exists()) {
                                  references.add(task.getResult().getReference());                      
                              }
                          }
                      }
                  });
              }
          } catch (Exception e) {
  
          } 
      }
  </code></pre>