简体   繁体   中英

How to query firestore with an OR-Query and realtime updates

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
    }
});

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM