title: Android merging two Firestore calls into single callback
body: <p>I'm currently querying a collection on Firestore link this one:</p>
  
  <pre><code>val missionsCollection = FirebaseFirestore.getInstance()
      .collection("/customers").document(user.customerId).collection("missions")
      .orderBy("deadline")
      .whereGreaterThanOrEqualTo("deadline", startDate)
      .whereLessThanOrEqualTo("deadline", endDate)
      .whereArrayContains("staffs", user.id)
  </code></pre>
  
  <p>Just to clear things up, on my mission document, the fields are, respectively:</p>
  
  <p><strong>deadline</strong>: a timestamp
  <br>
  <strong>staffs</strong>: an array of strings with the staffs ids</p>
  
  <p>So what I'm currently fetching are <strong>all missions from a certain period in time (<em>based on the deadline</em>) that belongs to a certain staff</strong>.</p>
  
  <p>But now, I also have to fetch <strong>missions that are visible to all staffs (<em>using the same deadline rule</em>)</strong>, which, on this application, means <strong>all mission where this array is empty</strong>.</p>
  
  <p>Since I don't have a method called <code>.whereArrayIsEmpty("staffs")</code>, I created a field on my document called "visibleToAllStaffs", which is a boolean field, but I have a deeper problem. Firebase won't allow a OR statement.</p>
  
  <p>I know I can call the first query and, on it's success callback, call a second one, but I would like to use something better than this, perhaps RxJava or Coroutines, but I'm getting stuck on how I could do this.</p>
  
  <p>So, my questions are:</p>
  
  <p>1) Is there a native way (using only firebase) for me to group two queries that fetches data from the same collection having only one callback for error and one for success (where, in this case, the success will bring both datas)?</p>
  
  <p>2) If not, can I solve it with RxJava or Coroutines? How?</p>
  
  <h2>EDIT</h2>
  
  <p>Just giving some more explanation, my second call would be:</p>
  
  <pre><code>val missionsCollection = FirebaseFirestore.getInstance()
      .collection("/customers").document(user.customerId).collection("missions")
      .orderBy("deadline")
      .whereGreaterThanOrEqualTo("deadline", startDate)
      .whereLessThanOrEqualTo("deadline", endDate)
      .whereEqualTo("visibleToAllStaffs", true)
  </code></pre>
  
  <p>The <code>missionsCollection</code> variables are objects of type <a href="https://firebase.google.com/docs/reference/js/firebase.firestore.Query" rel="nofollow noreferrer">Query</a>. On those objects I can call a <code>get()</code> method which returns a <a href="https://developers.google.com/android/reference/com/google/android/gms/tasks/Task" rel="nofollow noreferrer">Task</a> object of <a href="https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/QuerySnapshot" rel="nofollow noreferrer">QuerySnapshot</a> (<code>Task&lt;QuerySnapshot&gt;</code>)</p>
  
  <p>With this <code>Task&lt;QuerySnapshot&gt;</code> object, I can add some callbacks (success and error, for example), like this:</p>
  
  <pre><code>
  missionsCollection.get().addOnSuccessListener { documentSnapshot -&gt;
      val missions = documentSnapshot.map { it.toObject(Mission::class.java) }
      // do something with the list
  }.addOnFailureListener {
      // Do something
  }
  
  </code></pre>
  
  <p>My biggest problem with coroutines was trying to merge the callbacks, because I imagine I'll have something like this:</p>
  
  <pre><code>.merge(getMissionsCollection(), getSecondMissionsCollection())
  .addOnSuccessListener { documentSnapshot -&gt;
      val missionsFromBothCalls = documentSnapshot.map { it.toObject(Mission::class.java) }
      // do something with the full list
  }.addOnFailureListener {
      // Do something
  }
  </code></pre>
  
  <p>but I couldn't find any example like this one (where the methods return a "observable" and not the List I need and I got stuck on how to merge them together.</p>