Firebase使用equalsTo的where子句

时间:2017-02-28 10:48:03

标签: android firebase firebase-realtime-database

我有一个列表,列出了我想从Firebase下载的所有密钥。为此,我使用此代码:

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

newUidsQuery.addListenerForSingleValueEvent();

这是我目前的结构:

{
  "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"
    }
  }
}

问题在于,当我尝试仅获取具有特定键的条目时,我还获得具有该键作为"联系人"中的值的其他条目。领域。

1 个答案:

答案 0 :(得分:3)

当您调用orderBy...或其他方法时,它会返回一个新查询。因此,您现在正在为每个uid创建一个新查询,然后您不使用它。要保留它,您需要newUidsQuery = newUidsQuery.equalTo(uid)

但是这还不能解决问题,因为查询只能对单个属性和值进行排序/过滤。见Query based on multiple where clauses in firebase

更好的消息是,这并不重要,因为您可以一次检索一个项目。与您的预期不同,这并不比一次检索它们慢得多。请参阅Speed up fetching posts for my social network app by using query instead of observing a single event repeatedly或观看#AskFirebase的这一集:https://youtu.be/66lDSYtyils?t=1m49s