Firebase数据库无法正确同步 [英] Firebase Database Not Syncing Properly

查看:68
本文介绍了Firebase数据库无法正确同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个聊天应用程序.为此,我正在使用Firebase数据库.并使用FirebaseRecyclerAdapter显示消息.

I'm trying to make a chat application. For that I'm using firebase database. And to show messages using FirebaseRecyclerAdapter.

 mNewAdapter = new NewChatRecyclerAdapter(
                    ChatMessageItem.class,
                    R.layout.message_item,
                    RecyclerView.ViewHolder.class, ref.limitToLast(300).orderByChild("timestamp"), isAMod, publicroom, Fusername, booIsPrivateChat, chatid, otheruser);

要进行同步,

ref.limitToLast(300).orderByChild("timestamp").keepSynced(true);

但是,每次我重新打开应用程序时,加载所有新消息都需要3-5秒. 那我该如何解决这个问题.

But everytime I reopen the app it takes 3-5 seconds to load all new messages. So how can I fix this problem..

先感谢...

推荐答案

Firebase数据库仅在与服务器建立活动连接时才同步数据.这通常仅在应用程序处于活动状态时发生,因为Android在应用程序处于非活动状态时会关闭连接.

The Firebase Database only synchronizes data while there is an active connection to the server. This normally only happens when the app is active, since Android closes the connection when the app is inactive.

虽然存在连接,但Firebase SDK通常仅在您在该位置具有活动侦听器时才同步该位置中的数据.当您呼叫keepSynced(true)位置,当位置中没有活动的侦听器时,它也会同步.

While there is a connection, the Firebase SDK normally only synchronizes data in a location when you have an active listener in that location. When you call keepSynced(true) on a location, it will also synchronize when there is no active listener in the location.

对于应用程序中的主列表,例如在主活动中显示的数据,调用keepSynced(true)最为有用.通过保持同步,当您的用户位于其他屏幕上时,它也会被更新.这样,当他们回到主要活动时,即可立即获得数据.

Calling keepSynced(true) is most useful for the main list in your app, such as the data you show in your main activity. By keeping that synchronized, it will also be updated when your users are in a different screen. That way, when they come back to the main activity, the data is immediately available.

为减少用户在重新启动应用程序时必须等待的时间,您可以告诉Firebase通过调用setPersistenceEnabled(true) 将其缓存存储到磁盘.由于从磁盘加载通常比从网络加载更快,因此可以加快速度.

To reduce the time the user has to wait when restarting the app, you can tell Firebase to store its cache to disk by calling setPersistenceEnabled(true). Since loading from disk is typically faster than loading from network, this may speed things up.

这篇关于Firebase数据库无法正确同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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