我在试着做一个聊天应用。为此,我使用了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秒来加载所有新消息。那么我该如何解决这个问题呢..
先谢谢你...
发布于 2017-07-24 22:11:03
Firebase数据库仅在与服务器有活动连接时才同步数据。这通常只有在应用程序处于活动状态时才会发生,因为Android会在应用程序处于非活动状态时关闭连接。
当存在连接时,Firebase SDK通常只在某个位置有活动侦听器时才同步该位置的数据。当你call keepSynced(true) on a location, it will also synchronize when there is no active listener in the location的时候。
调用keepSynced(true)对于应用程序中的主列表最有用,比如在主活动中显示的数据。通过保持同步,当你的用户在不同的屏幕上时,它也会被更新。这样,当他们返回到主要活动时,数据立即可用。
要减少用户在重启应用程序时必须等待的时间,您可以使用tell Firebase to store its cache to disk by calling setPersistenceEnabled(true)。由于从磁盘加载通常比从网络加载更快,因此这可能会加快速度。
发布于 2017-07-24 13:10:43
启用离线功能,以便您的应用程序能够缓存这些数据
FirebaseDatabase.getInstance().setPersistenceEnabled(true);https://stackoverflow.com/questions/45271543
复制相似问题