如何在Firebase实时数据库中使用onChildRemoved? [英] How to use onChildRemoved in Firebase Realtime Database?
问题描述
好的,所以我想知道onChildRemoved
在Firebase Realtime Databse中的工作方式.我可以在用户实时添加数据后立即添加数据.但是当用户删除数据时,我想做同样的事情.如何进行呢?我阅读了文档,但无法获取.
Okay so I wanted to know how the onChildRemoved
works in Firebase Realtime Databse. I am able to add data as soon as the user adds the data i.e., in realtime. But I want to do the same thing when the user deletes the data. How to proceed with that? I read the documentation but could not get it.
任何帮助将不胜感激.
Any help will be appreciated.
PS:我可以通过mRootRef.child("message").removeValue();
删除数据,但我必须返回并再次参加此活动以查看更改.
PS: I am able to remove the data by mRootRef.child("message").removeValue();
but I have to go back and come again to this activity to see the changes.
private void loadmessage(String class_id, String email_red) {
messageQuery.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Message message = dataSnapshot.getValue(Message.class);
messageList.add(message);
mAdapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
//what to add here??
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
推荐答案
@ mark922的答案具有要点,但您还需要一些步骤.
The answer from @mark922 has the gist, but you'll need a few more steps.
当从数据库中删除一条消息时,您会在onChildRemoved
中得到该已删除消息的DataSnapshot
.您将需要:
When you remove a message from the database, you'll get a DataSnapshot
of that removed message in onChildRemoved
. You will need to:
- 在您的
messageList
中找到相应的消息. - 从列表中删除邮件.
- 调用
mAdapter.notifyDataSetChanged()
使适配器重新绘制列表/回收器视图.
- Find the corresponding message in your
messageList
. - Remove the message from the list.
- Call
mAdapter.notifyDataSetChanged()
to make the adapter repaint the list/recycler view.
棘手的是步骤1,因为这意味着您需要能够找到消息.最简单的方法是为每个消息保留DataSnapshot.getKey()
.由于每个子节点的密钥必须唯一,因此保证每个密钥只有一条消息.
The tricky bit is step 1, because it means you need to be able to find the message. The easiest way to do this is to keep the DataSnapshot.getKey()
for each message. Since the key of each child node must be unique, there is guaranteed to only be a single message for each key.
将其添加到当前代码中的一种简单方法是添加keyList
:
A simple way to add this to your current code, is to add a keyList
:
List<String> keyList = new ArrayList<String>();
然后在添加值t messageList
时将每个DataSnapshot
的键添加到该列表:
And then add the key of each DataSnapshot
to that list as you add the value t messageList
:
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Message message = dataSnapshot.getValue(Message.class);
messageList.add(message);
keyList.add(dataSnapshot.getKey());
mAdapter.notifyDataSetChanged();
}
现在您有一个keyList
和messageList
,并且每个索引处的项目都彼此对应.因此keyList[0]
中的键对应于messageList[0]
中的值,等等.
Now you have a keyList
and a messageList
and the items at each index correspond to each other. So the key in keyList[0]
corresponds to the value in messageList[0]
, etc.
这意味着在onChildRemoved
中,您可以查找在keyList
中删除的邮件的索引,然后根据索引从messageList
和keyList
中删除该邮件:
That means that in onChildRemoved
you can look up the index for the message that was removed in keyList
and then remove that message from both messageList
and keyList
based on the index:
public void onChildRemoved(DataSnapshot dataSnapshot) {
int index = keyList.indexOf(dataSnapshot.getKey());
messageList.remove(index);
keyList.remove(index);
mAdapter.notifyDataSetChanged();
}
关于邮件插入的说明
仅当新消息始终附加在列表末尾时,以上代码才有效.在一个简单的聊天应用中,这可能是正确的.但是在更先进的应用程序中,您也可以在列表中的其他位置插入数据.
A note on message insertions
The above code works only when new messages are always appended to the end of the list. In a simple chat app that may be true. But in more evolved apps, you may also insert data somewhere else in the list.
在这种情况下,传递到onChildAdded
的String s
变得很重要,因为它实际上是String previousChildKey
:插入新子级之前的子级 的键.为了正确处理此情况,您应该跟踪项目的顺序,例如,通过将键和值插入到它们各自列表中的正确索引中来进行跟踪.那时,您还需要处理onChildChanged
和(对于这种情况更重要)onChildMoved
.
In that case the String s
that is passed into onChildAdded
becomes important, since it is actually String previousChildKey
: the key of the child before which the new child was inserted. To handle this scenario correctly, you should track the order of the items, for example by inserting the key and value in the correct index in their respective lists. At that point you'll also want to handle onChildChanged
and (more important for this scenario) onChildMoved
.
All of this is not incredibly complex, but it's quite easy to get something wrong. I'd recommend having a look at the FirebaseArray
class from FirebaseUI as an example/source of inspiration.
这篇关于如何在Firebase实时数据库中使用onChildRemoved?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!