使用推送

时间:2017-07-20 07:10:54

标签: android firebase firebase-realtime-database

我正在尝试更新Firebase数据库中的值。这是数据库的结构:

enter image description here

如果status"accepted"具有特定值,我需要将r_id的值更新为s_id。这里的问题是每个friend_data孩子的关键是使用" push"生成的。像这样

db.child("friend_data").push().setValue(friend);

我尝试了this question herethis one here,但都不符合我的要求。我该如何解决这个问题?

编辑:我现在明白我应该在这里使用Query。这就是我试过的:

final DatabaseReference db = FirebaseDatabase.getInstance().getReference("friend_data");

Query query = db.orderByChild("r_id").equalTo(f_id).orderByChild("s_id").equalTo(id);
query.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot child : dataSnapshot.getChildren()) {
            db.child(child.getKey()).child("status").setValue("accepted");
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

这不起作用,因为提出了这个错误

java.lang.IllegalArgumentException:您无法合并多个orderBy来电!

那么如何组合两个查询?

1 个答案:

答案 0 :(得分:2)

经过几天的努力,我终于得到了这个工作。

我添加了一个带有orderByChild方法的查询,以便在服务器端过滤掉大量数据(感谢这个answer的想法。)我得到的剩余数据为{{数据类型child的1}}具有我需要的所有必要信息。这是查询

DataSnapshot

但我再次无法满足我的要求,因为我实际上需要修改Query query = db.orderByChild("r_id").equalTo(f_id); query.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { for (DataSnapshot child : dataSnapshot.getChildren()) { String child_s_id = (String) child.child("s_id").getValue(); String child_status = (String) child.child("status").getValue(); if (child_s_id.equals(id)) { Log.e("Got value", f_id + " - x -" + id + " " + child_status); } } } @Override public void onCancelled(DatabaseError databaseError) { } }); 中的一个值而我无法使用child之类的任何方法来更改{{1}的值}}

同样,我花了很长时间(多么白痴)才弄明白我必须将setValue(数据类型child)转换为child的引用工作。它工作了

DataSnapShot

这是完成的代码。希望它可以帮助一路上的人

setValue
相关问题