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" : "lung.razvan@yahoo.com",
      "name" : "Razvan Cristian Lung",
      "photoUrl" : "https://lh5.googleusercontent.com/-bItm3-ieAtU/AAAAAAAAAAI/AAAAAAAALZo/mtPyAMohOvg/s96-c/photo.jpg",
      "uid" : "5mvsiNKz2hO4rmcDDNskv855dkB3"
    },
    "GG8JeRNOIhb1qloZb4oCAb7Jd593" : {
      "contactsHash" : 1,
      "email" : "andralung@yahoo.com",
      "name" : "Andra Florina Lung",
      "photoUrl" : "https://lh4.googleusercontent.com/-po2yelyi3mY/AAAAAAAAAAI/AAAAAAAAQ5s/ROefxP6Q1oA/s96-c/photo.jpg",
      "uid" : "GG8JeRNOIhb1qloZb4oCAb7Jd593"
    },
    "gc0ci7Jgu2QpVYFbeiMJfVy1WHP2" : {
      "contactsHash" : 1,
      "email" : "lung_razvan2100@yahoo.com",
      "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.

Razvan Cristian Lung
  • 5,661
  • 5
  • 25
  • 49

1 Answers1

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

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 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" :)) – Razvan Cristian Lung Feb 28 '17 at 16:19
  • 1
    lol... that Evanghelos thing will haunt me for years. :-) . FirebaseUI has an example adapter for handling these joins: https://github.com/firebase/FirebaseUI-Android/tree/master/database#using-firebaseui-with-indexed-data – Frank van Puffelen Feb 28 '17 at 16:53