gpt4 book ai didi

java - Firebase 数据库未正确同步

转载 作者:搜寻专家 更新时间:2023-11-01 07:45:29 26 4
gpt4 key购买 nike

我正在尝试制作一个聊天应用程序。为此,我正在使用 firebase 数据库。并使用 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 秒才能加载所有新消息。那么我该如何解决这个问题..

提前致谢...

最佳答案

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

虽然存在连接,但 Firebase SDK 通常仅在您在某个位置有 Activity 监听器时才同步该位置的数据。当你call keepSynced(true) on a location, it will also synchronize when there is no active listener in the location .

调用 keepSynced(true) 对您应用中的主列表最有用,例如您在主 Activity 中显示的数据。通过保持同步,当您的用户位于不同屏幕时,它也会更新。这样,当他们回到主要 Activity 时,数据立即可用。

要减少用户在重新启动应用程序时必须等待的时间,您可以 tell Firebase to store its cache to disk by calling setPersistenceEnabled(true) .由于从磁盘加载通常比从网络加载更快,这可能会加快速度。

关于java - Firebase 数据库未正确同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45271543/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com