我有一个使用 Firebase Android SDK 作为服务器的聊天项目。 3 个月后,我的应用程序有很多用户,他们互相聊天很多。从 Firebase 加载所有聊天列表需要很长时间。所以现在,我想知道如何使用 Android SDK 从 Firebase 进行分页查询。
我研究了这个link查找此问题的查询命令但失败。我只查询第一行或最后一行的一些行。我无法从 Firebase 中的随机定位进行查询。使用skip
和take
键查询的示例。
如果您有任何相关文件。请告诉我。 感谢您的提前。
最佳答案
skip()
或 take()
等概念不能很好地映射到 Firebase 数据库的实时世界。这就是为什么分页也很难实现的原因。从技术上讲这是可能的,但为了获得良好的用户体验,请务必记住页面上的数据可能会在用户查看时发生变化。
回到如何实现分页:startAt()
和 endAt()
方法采用可选的第二个参数,记录为(强调我的):
public Query startAt (String value, String key)
Create a query constrained to only return child nodes with a value greater than or equal to the given value, using the given
orderBy
directive or priority as default, and additionally only child nodes with a key greater than or equal to the given key.
因此,例如,如果您有一个按日期排序的项目列表,您可以传入上一页最后一项的日期和键以获取下一页。您将不得不请求一个额外的项目(通常称为 anchor 项目),但开销应该很小。
关于android - 如何使用 Android FirebaseUI 从 Firebase 分页查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41278544/