问题描述
我正在使用firebase编写一个聊天应用程序.这里有一个问题,如果设备处于脱机状态,则数据存储在缓存中,并且当设备重新联机时,缓存将被同步,但是如何知道哪个数据已同步且未同步(用户可能没有良好的Internet连接,因此同步可能会延迟).如何通知用户消息已发送或正在发送,我们也需要管理大量消息谢谢!
I'm writing a chat application with the help of firebase.Here I have a problem , if the device is offline the data is stored in cache and when device is back online the cache will be synced , but how to know which data is synced and which not (User may have poor internet connection , so syncing may be delayed).How to notify user that message has been sent or sending , and that too we need to manage a huge list of messagesThank you!
推荐答案
有两种方法:
-
使用完成侦听器,如下所示:只要应用保持活动状态,此方法就可以工作.完成侦听器无法在应用程序重启后幸存下来,因此,如果应用程序重启,它将无法正常工作.
Using a completion listener as shown here: Firebase Android - how to tell if node has been synced This approach works as long as the app stays active. Completion listener don't survive an app restart, so it won't work if the app is restarted.
使用前哨值.确保从单个客户端到数据库的写操作按顺序执行.使用此知识,您可以在重新启动应用程序或恢复连接时写一个前哨值(认为:虚拟),并检测何时编写该值.一旦确认了哨兵值,就可以确定所有较旧的消息也已由服务器处理.
Using a sentinel value. Writes to the database from a single client are guaranteed to be executed in order. Using this knowledge, you could write a sentinel (think: dummy) value when the app is restarted, or the connection is restored, and detect when that one is written. Once the sentinel value is confirmed, you can be certain that all older messages have also been handled by the server.
另请参阅:
这篇关于如何知道哪些离线数据已同步到Firebase实时数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!