问题描述
我正在将Firebase for Android用于我们应用程序的聊天组件.我在弄清楚如何可靠地在每个聊天消息上实现状态更新时遇到了麻烦.例如,当聊天与服务器同步时显示正在发送..",并在同步后获得成功反馈.
I'm using Firebase for Android for the chat component of our app.I'm having trouble figuring out how to reliably implement status updates on each chat message.For example, showing "Sending.." when the chat is being synced with the server, and having a success feedback after sync.
我有一个onChildAdded侦听器,可将消息提供给我的适配器.但是,当在本地添加每个节点时,将立即触发此侦听器,而我无法检查每个节点的状态
I have a onChildAdded listener that supplies the messages to my adapter. However, this listener is fired immediately when each node is added locally, and I can't check the status of each node
我当前的解决方案是保留一组节点密钥,并在我将某些内容推送到Firebase时添加密钥.然后在setValue回调中,从集合中删除节点键.但是,这是非常不可靠的,因为在销毁呼叫活动等之后可以同步节点.
My Current solution is to keep a set of node keys, and add keys whenever I push something to Firebase. Then on the setValue callback, I remove the node key from the set. However, this is very unreliable since the nodes can be synced when the calling activity has been destroyed, etc.
我想知道是否有一种更简单的方法来检查每个节点是否已同步到服务器?
I am wondering if there is a simpler way to check if each node has been synced to the server?
谢谢!
推荐答案
使用此方便的代码示例:
With this handy code sample:
ref.setValue("I'm writing data", new Firebase.CompletionListener() {
@Override
public void onComplete(FirebaseError firebaseError, Firebase firebase) {
if (firebaseError != null) {
System.out.println("Data could not be saved. " + firebaseError.getMessage());
} else {
System.out.println("Data saved successfully.");
}
}
});
这篇关于Firebase Android-如何判断节点是否已同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!