1

I have a list of all the keys I want to download from Firebase. For this I'm using this code:

Query newUidsQuery = FirebaseDatabaseHelper.getUsersReference();
newUidsQuery.orderByChild(DATABASE_NODE_USER_UID);
        for (String uid : uidList) {
            newUidsQuery.equalTo(uid);
        }

newUidsQuery.addListenerForSingleValueEvent();

This is my current structure:

{
  "users" : {
    "5mvsiNKz2hO4rmcDDNskv855dkB3" : {
      "contacts" : [ "GG8JeRNOIhb1qloZb4oCAb7Jd593", "gc0ci7Jgu2QpVYFbeiMJfVy1WHP2" ],
      "contactsHash" : -224276455,
      "email" : "[email protected]",
      "name" : "Razvan Cristian Lung",
      "photoUrl" : "https://lh5.googleusercontent.com/-bItm3-ieAtU/AAAAAAAAAAI/AAAAAAAALZo/mtPyAMohOvg/s96-c/photo.jpg",
      "uid" : "5mvsiNKz2hO4rmcDDNskv855dkB3"
    },
    "GG8JeRNOIhb1qloZb4oCAb7Jd593" : {
      "contactsHash" : 1,
      "email" : "[email protected]",
      "name" : "Andra Florina Lung",
      "photoUrl" : "https://lh4.googleusercontent.com/-po2yelyi3mY/AAAAAAAAAAI/AAAAAAAAQ5s/ROefxP6Q1oA/s96-c/photo.jpg",
      "uid" : "GG8JeRNOIhb1qloZb4oCAb7Jd593"
    },
    "gc0ci7Jgu2QpVYFbeiMJfVy1WHP2" : {
      "contactsHash" : 1,
      "email" : "[email protected]",
      "name" : "Lung Razvan",
      "photoUrl" : "https://scontent.xx.fbcdn.net/v/t1.0-1/p100x100/15390976_1192204140865562_3397773349261436244_n.jpg?oh=d61795a8df67d3e9c5ddf60557e9e60c&oe=59270863",
      "uid" : "gc0ci7Jgu2QpVYFbeiMJfVy1WHP2"
    }
  }
}

The problem is that when I try to get only the entries with the specific key I also get the other entries that have that key as a value in the "contacts" field.

2
  • Try replacing orderByChild with orderByKey(). Is that the result that you want? Commented Feb 28, 2017 at 10:50
  • 1
    same result, in fact that was my first try Commented Feb 28, 2017 at 11:16

1 Answer 1

3

When you call orderBy... or other methods, it returns a new query. So you're now creating a new query for each uid that you then don't use. To keep it, you'd need newUidsQuery = newUidsQuery.equalTo(uid).

But that won't solve the problem yet, because a query can only order/filter on a single property and value. See Query based on multiple where clauses in firebase.

Better news is that this doesn't matter much here, since you can just retrieve the items one at a time. Unlike what you might expect, that's not significantly slower than retrieving them in one go. See Speed up fetching posts for my social network app by using query instead of observing a single event repeatedly or watch this episode of #AskFirebase: https://youtu.be/66lDSYtyils?t=1m49s

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for you answer. I've taken the Firebase in a Weekend course and I really liked it and I saw that FireCast. Thanks again. The thing is that I need to get then all at one because I want to update a RecyclerView Adapter. And I don't want to send and update request every time a new item arrives. But if this is not possible I will stick with calling one by one. Thanks again, Puff! "Evanghelos" :))
lol... that Evanghelos thing will haunt me for years. :-) . FirebaseUI has an example adapter for handling these joins: github.com/firebase/FirebaseUI-Android/tree/master/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.