Firebase Android-如何判断节点是否已同步 [英] Firebase Android - how to tell if node has been synced

查看:118
本文介绍了Firebase Android-如何判断节点是否已同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将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?

谢谢!

推荐答案

来自

如果您想知道何时提交数据,可以添加完成侦听器. setValue()和updateChildren()都带有一个可选的完成侦听器,当写入已提交到数据库时将调用该侦听器.

If you'd like to know when your data has been committed, you can add a completion listener. Both setValue() and updateChildren() take an optional completion listener that is called when the write has been committed to the database.

使用此方便的代码示例:

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-如何判断节点是否已同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆