使用推送创建的节点更新Firebase数据库 [英] Updating Firebase Database with nodes created with push
问题描述
如果 r_id $>,我需要更新
status
的值到accepted
c $ c>和 s_id
具有特定的值。这里的问题是每个 friend_data
子项的关键字都是使用push来生成的,像这样
。 db.child( friend_data)推()的setValue(朋友);
编辑:现在我明白我应该使用 Query
在这里。这是我试过的:
$ p $ 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 DataEventListener(){
@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调用!
所以,如何组合两个查询?我用 orderByChild
方法添加了一个Query,以便在服务器端自己过滤掉一大块数据(感谢这个 child 数据类型<$ c的其余数据$ c> DataSnapshot 具有我需要的所有必要信息。这里是查询
Query query = db.orderByChild(r_id)。equalTo(f_id);
query.addListenerForSingleValueEvent(new DataEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
for(DataSnapshot child:dataSnapshot.getChildren()){
字符串child_s_id =(String)child.child(s_id)。getValue();
字符串child_status =(String)child.child(status)。getValue();
$ b $如果(child_s_id.equals(id)){
Log.e(Got value,f_id + - x - + id ++ child_status);
}
}
}
@Override
public void onCancelled(DatabaseError databaseError){
});
但是我再也达不到我的要求,因为我实际上需要修改其中的一个值 再次,我花了很长时间(这是一个白痴)弄清楚,我必须转换 这里是完整的代码。希望它帮助沿途的人 I am trying to update a value in my Firebase database. This is the structure of the database: I need to update the value of I have tried this question here and this one here but both don't meet my requirements. How do I go about solving this one? Edit: I understand now that I am supposed to use a This does not work either because this error is raised java.lang.IllegalArgumentException: You can't combine multiple orderBy calls!. So how do I combine two Queries? I have finally got this to work after days of trying to figure it out. I added a Query with But again I could not meet my requirements, because I actually needed to modify one of the value in the Again, it took me a long while (what an idiot) to figure out that I had to convert the Here is the completed code. Hope it helps someone along the way
这篇关于使用推送创建的节点更新Firebase数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! child
我不能使用像 setValue
这样的方法来改变 child < code $。
子(数据类型
DataSnapShot
)引用到 setValue
的引用上。它的工作原理
child.getRef()。child(status)。setValue(accepted);
Query query = db.orderByChild(r_id)。equalTo(f_id);
query.addListenerForSingleValueEvent(new DataEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
for(DataSnapshot child:dataSnapshot.getChildren()){
字符串child_s_id =(String)child.child(s_id)。getValue();
字符串child_status =(String)child.child(status)。getValue();
if(child_s_id.equals(id))
child.getRef()。child(status)。setValue(accepted);
}
}
@Override
public void onCancelled(DatabaseError databaseError){
$ b}
});
status
to "accepted"
if the r_id
and s_id
have a specific value. The problem here is that the key for each friend_data
child is generated using "push" like thisdb.child("friend_data").push().setValue(friend);
Query
here. This is what I tried: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) {
}
});
orderByChild
method to filter out a big chunk of data at the server side itself (thanks to this answer for the idea.) The remaining data which I got as child
of data type DataSnapshot
had all the necessary information that I needed. Here is the queryQuery 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
and I could not use any methods like setValue
to change the value of child
.child
(of data type DataSnapShot
) to a reference for setValue
to work. And it worked child.getRef().child("status").setValue("accepted");
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))
child.getRef().child("status").setValue("accepted");
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});