0

i have a firestore Query like so:

docRef.whereEqualTo("A",1).whereEqualTo("B",3) OR docRef.whereEqualTo("A",2).whereEqualTo("C",3)

As this is not possible with a single query in firebase I'd like to merge them clientside. I found a post on SO where you just use the task objects and wait for them to complete. But i really do want to have the ListenerRegistration and use that query for live updates. What is the best way to get life updates for the combined or query.

BTW: This is the code i was talking about (Taken from https://stackoverflow.com/a/50132229)

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
Query firstQuery = rootRef...
Query secondQuery = rootRef...

Task firstTask = firstQuery.get();
Task secondTask = secondQuery.get();

Task combinedTask = Tasks.whenAllSuccess(firstTask, secondTask).addOnSuccessListener(new OnSuccessListener<List<Object>>() {
    @Override
    public void onSuccess(List<Object> list) {
         //Do what you need to do with your list
    }
});
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Avinta
  • 678
  • 1
  • 9
  • 26
  • 2
    Add a listener to each query, and have them all call into a common function to handle each query result. Your code should be prepared to receive results in any order, at any rate. – Doug Stevenson Jun 11 '20 at 22:01
  • 1
    Can you elaborate a bit more? I think I use a similar approach which felt really hacky to me. I have a global TreeSet where I push the results of each query into. In both of the callbacks I then return the TreeSet which will by default not have any duplicates. But I have to maintain the global set? Think this is not what you were referring to. But by the way. Thank you for your really amazing YouTube videos on cloud functions. My first time I had to use nodejs and firebase and you saved my day. Really on the point. – Avinta Jun 11 '20 at 22:23
  • I don't think there is a less hacky way to go. – Doug Stevenson Jun 11 '20 at 22:31

1 Answers1

1

As talked over the comments, there isn't a better or less "hacky" to merge queries, as using listeners at each queries and handling the results in the way that they are returned. Due to the fact that queries in Firestore are shallow, all the queries needed, that go this kind of "extra mile", beyond what it's directly available, might sound and look "hacky", but they are, usually, the only solutions for more complex situations.

For this situation, there isn't actually a really good or better option, so, to summarize, either using the option of @DougStevenson might work for you or continue to use your approach, in case it worked for you.

gso_gabriel
  • 4,199
  • 1
  • 10
  • 22