在Firebase中使用equalTo(“value”,“key”)? [英] Usage of equalTo("value","key") in Firebase?

查看:126
本文介绍了在Firebase中使用equalTo(“value”,“key”)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是数据结构:

  {
list:{
-
.priority:-1.449077948445E12,
date:1449077948445,
id:0,
name:Name_0

-K4YlfoWHZqPyWONv68Z:{
.priority:-1.449077948445E12,
date:1449077948445,
id:1 ,
name:Name_1
},
-K4YlfoWHZqPyWONv68_:{
.priority:-1.449077948445E12,
date:1449077948445 ,
id:0,
name:Name_2
},
-K4YlfoWHZqPyWONv68a​​:{
.priority 1.449077948445E12,
date:1449077948445,
id:1,
name:Name_3
},
...



$ b $ p
$ b

我只需要得到id为0的项;



以下是工作:

 最终Firebase ref = new Fi变基( https://millezim-test.firebaseio.com/).child( 名单); 

ref.orderByChild(id)。equalTo(1)。addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){ (DataSnapshot d:dataSnapshot.getChildren()){
Model m = d.getValue(Model.class);
}
}

@覆盖
public void onCancelled(FirebaseError firebaseError){
$ b}
});

但是不是这个,为什么?

  ref.equalTo(1,id)。addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){$ b (DataSnapshot d:dataSnapshot.getChildren()){
Model m = d.getValue(Model.class);
}
}

@Override
public void onCancelled(FirebaseError firebaseError){


}); b


$ b我是否误解了equalTo(

firebase文档for equalTo 对此进行了说明(可选)参数:


从以前指定的优先级的子元素开始的子键。

由于您没有按优先顺序排序,您需要指定孩子显式调用 orderByChild()


Here is the following data structure :

{
  "list" : {
    "-K4YlfoWHZqPyWONv68Y" : {
      ".priority" : -1.449077948445E12,
      "date" : 1449077948445,
      "id" : "0",
      "name" : "Name_0"
    },
    "-K4YlfoWHZqPyWONv68Z" : {
      ".priority" : -1.449077948445E12,
      "date" : 1449077948445,
      "id" : "1",
      "name" : "Name_1"
    },
    "-K4YlfoWHZqPyWONv68_" : {
      ".priority" : -1.449077948445E12,
      "date" : 1449077948445,
      "id" : "0",
      "name" : "Name_2"
    },
    "-K4YlfoWHZqPyWONv68a" : {
      ".priority" : -1.449077948445E12,
      "date" : 1449077948445,
      "id" : "1",
      "name" : "Name_3"
    },
 ...
}

I need to only get item where id is "0";

The following is working :

final Firebase ref = new Firebase("https://millezim-test.firebaseio.com/").child("list");

ref.orderByChild("id").equalTo("1").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot d : dataSnapshot.getChildren()) {
            Model m = d.getValue(Model.class);
        }
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {

        }
});

But not this one, why ?

ref.equalTo("1","id").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot d : dataSnapshot.getChildren()) {
            Model m = d.getValue(Model.class);
        }
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {

        }
});

Did I misunderstand that equalTo("value","key") is to use without reordering By something ?

解决方案

The Firebase documentation for equalTo says this about the (optional) key parameter:

The child key to start at, among the children with the previously specified priority. This argument is only allowed if ordering by priority.

Since you're not ordering by priority, you'll need to specify the child with an explicit call to orderByChild().

这篇关于在Firebase中使用equalTo(“value”,“key”)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆