[英]Firebase Database overwrites previous value with setValue()
So I have a Database in Firebase. 所以我在Firebase中有一个数据库。
This code is in onCreate(): 这段代码在onCreate()中:
DatabaseReference mDatabaseRefUser =FirebaseDatabase.getInstance().getReference("Users");
This code is in another method that gets called onClick: 这段代码在另一个称为onClick的方法中:
mDatabaseRefUser.child("Chat").child(mAuth.getCurrentUser().getUid())
.child(userID).child(uploadID).child("Messages").push().setValue(new ChatMessage(addMessageEditText.getText().toString(),
mAuth.getCurrentUser().getEmail()));
mDatabaseRefUser.child("Chat").child(userID)
.child(mAuth.getCurrentUser().getUid()).child(uploadID).child("Messages").push().setValue(new ChatMessage(addMessageEditText.getText().toString(),
userEmail));
What im trying to do is to add a new value without deleting the previous/existing value everytime the method gets called. 我试图做的是每次调用该方法时都添加一个新值,而不删除先前/现有的值。
For the moment everytime I click and start the method, the previous/existing value gets removed and the new one gets added. 每次单击并启动该方法时,先前/现有的值都会被删除,而新值将被添加。 But I want neither to be deleted.
但是我都不希望被删除。 I want this the new value to be added aswell.
我希望这个新值也能被添加。 I have tried with getRef and push() but my data still gets overwritten.
我已经尝试过使用getRef和push(),但是我的数据仍然被覆盖。 What am I doing wrong here?
我在这里做错了什么?
This is my JSON 1st time I call the method: 这是我第一次调用JSON方法时:
"Users" : {
"Chat" : {
"Xta3jwm4yLQWaJBypMBFt2esiOr2" : {
"xfqAFlRpbUZRlZb76svN5FUtbU93" : {
"-LCvIA_9L1Y9rjkm96Aj" : {
"Messages" : {
"-LCvqDidnNqQ8ALZhDud" : {
"messageText" : "Very good",
"messageTime" : 1526791230552,
"messageUser" : "mama@mama.com"
}
},
"chatAgainstUserEmail" : "mama2@mama.com",
"chatAgainstUserID" : "xfqAFlRpbUZRlZb76svN5FUtbU93",
"uploadID" : "-LCvIA_9L1Y9rjkm96Aj",
"userEmail" : "mama@mama.com",
"userID" : "Xta3jwm4yLQWaJBypMBFt2esiOr2"
}
}
},
"xfqAFlRpbUZRlZb76svN5FUtbU93" : {
"Xta3jwm4yLQWaJBypMBFt2esiOr2" : {
"-LCvIA_9L1Y9rjkm96Aj" : {
"Messages" : {
"-LCvqDigI8ZwHDYuPhw9" : {
"messageText" : "Very good",
"messageTime" : 1526791230555,
"messageUser" : "mama2@mama.com"
}
},
"chatAgainstUserEmail" : "mama@mama.com",
"chatAgainstUserID" : "Xta3jwm4yLQWaJBypMBFt2esiOr2",
"uploadID" : "-LCvIA_9L1Y9rjkm96Aj",
"userEmail" : "mama2@mama.com",
"userID" : "xfqAFlRpbUZRlZb76svN5FUtbU93"
}
}
}
}
This is my JSON when I call it for the 2nd time 当我第二次调用它时,这是我的JSON
"Users" : {
"Chat" : {
"Xta3jwm4yLQWaJBypMBFt2esiOr2" : {
"xfqAFlRpbUZRlZb76svN5FUtbU93" : {
"-LCvIA_9L1Y9rjkm96Aj" : {
"Messages" : {
"-LCvr0zoHOPxsKOVJzeu" : {
"messageText" : "Very good 2nd call",
"messageTime" : 1526791440547,
"messageUser" : "mama@mama.com"
}
},
"chatAgainstUserEmail" : "mama2@mama.com",
"chatAgainstUserID" : "xfqAFlRpbUZRlZb76svN5FUtbU93",
"uploadID" : "-LCvIA_9L1Y9rjkm96Aj",
"userEmail" : "mama@mama.com",
"userID" : "Xta3jwm4yLQWaJBypMBFt2esiOr2"
}
}
},
"xfqAFlRpbUZRlZb76svN5FUtbU93" : {
"Xta3jwm4yLQWaJBypMBFt2esiOr2" : {
"-LCvIA_9L1Y9rjkm96Aj" : {
"Messages" : {
"-LCvr0zrh8wQ2WurUezs" : {
"messageText" : "Very good 2nd call",
"messageTime" : 1526791440550,
"messageUser" : "mama2@mama.com"
}
},
"chatAgainstUserEmail" : "mama@mama.com",
"chatAgainstUserID" : "Xta3jwm4yLQWaJBypMBFt2esiOr2",
"uploadID" : "-LCvIA_9L1Y9rjkm96Aj",
"userEmail" : "mama2@mama.com",
"userID" : "xfqAFlRpbUZRlZb76svN5FUtbU93"
}
}
}
}
I have also tried with getRef().push()
, but that didn't work either. 我也尝试过使用
getRef().push()
,但这也不起作用。 I have also tried without . 我也试过了。
getRef()
and without .push()
, didnt work either. getRef()
和没有.push()
任何一个都不起作用。
To update a value instead of overriding it, you should use updateChildren() method. 要更新值而不是覆盖它,应使用updateChildren()方法。
You are overriding it because every node in a Firebase database is a Map
and in the case of Map
, it replaces the old value with the new one. 之所以要覆盖它,是因为Firebase数据库中的每个节点都是
Map
,对于Map
,它将新值替换为旧值。
To update data you should go for such kind of code instead of using child.setValue()
as its name suggests it will directly replace old values with new values. 要更新数据,您应该使用此类代码,而不要使用
child.setValue()
,顾名思义,它将直接用新值替换旧值。 But If you want to change a specific value then you should go for something as below. 但是,如果要更改特定值,则应进行以下操作。 Give it a try mate!
给它一个尝试伴侣!
HashMap<String, Object> params = new HashMap<>();
params.put("yourKey", your value);
yourDatabaseChildReference.updateChildren(params);
My only solution to this issue was to make a new Database, 我对此问题的唯一解决方案是创建一个新的数据库,
I added this: 我添加了这个:
mDatabaseRefUser.child("Chatting").child(mAuth.getCurrentUser().getUid())
.child(userID).child(uploadID).child("Messages").push().setValue(new ChatMessage(addMessageEditText.getText().toString(),
mAuth.getCurrentUser().getEmail()));
mDatabaseRefUser.child("Chatting").child(userID)
.child(mAuth.getCurrentUser().getUid()).child(uploadID).child("Messages").push().setValue(new ChatMessage(addMessageEditText.getText().toString(),
userEmail));
and removed the old one 并删除旧的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.