title: How to count the number of documents under a collection in Firestore?
body: <p>I am trying to get <a href="https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/CollectionReference" rel="noreferrer">CollectionReference</a> count that exists on the Cloud Firestore, I have tried to get it with:</p>
  
  <pre><code>FirebaseFirestore db = FirebaseFirestore.getInstance();
      final CollectionReference postsCollection = db.collection("Posts");
  
      final TaskCompletionSource&lt;Integer&gt; source = new TaskCompletionSource&lt;&gt;();
      new Thread(new Runnable() {
          @Override
          public void run() {
              int fromWhereToStart = postsCollection.get().getResult().size();
              source.setResult(fromWhereToStart);
          }
      }).start();
  
      Task&lt;Integer&gt; task = source.getTask();
      task.addOnCompleteListener(new OnCompleteListener&lt;Integer&gt;() {
          @Override
          public void onComplete(@NonNull Task&lt;Integer&gt; task) {
              Log.e("Z_fromWhereToStart", "= " + task.getResult());
          }
      });
  </code></pre>
  
  <p>But unfortunately, I'm getting:</p>
  
  <pre><code>java.lang.IllegalStateException: Task is not yet complete
  </code></pre>
  
  <p>Is there is another way to get the count of another way to fix the <code>IllegalStateException</code>?</p>