Firebase更好地获取记录总数 [英] Firebase better way of getting total number of records

查看:119
本文介绍了Firebase更好地获取记录总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从交易文档第二段:


这里的意图是让客户增加
的总数聊天消息发送(忽略了一会儿有更好的方式
实现这一点)。

什么是一些标准的更好的方式实现这一点?

特别是,我正在试图做的事情,如检索最近的50个记录。这要求我从列表的末尾开始,所以我需要一种方法来确定最后一条记录是什么。



我看到它们的选项:




  • 每次添加记录时使用事务更新计数器,使用计数器值和setPriority()进行排序
  • forEach()父类并读取所有记录,在客户端执行我自己的排序/过滤
  • 编写服务器代码以分析Firebase表并创建索引列表,如mostRecent Messages和totalNumberOfMessages



我是否缺少明显的选择?

解决方案

  

要查看列表中的最后50条记录,只需调用limit > var data = new Firebase(...);
data.limit(50).on(...);

Firebase元素首先按优先级排序,如果优先级匹配(或者没有设置)名称。 push()命令会自动创建按时间顺序排列的元素,所以如果使用push(),则不需要额外的工作来使用limit()。

要计算列表中的元素,我会建议添加一个值回调,然后遍历快照(或者采用我们提到的事务方法)。文档中的注释实际上是指一些即将到来的功能,我们还没有发布,这将允许您在不加载它们的情况下对其进行计数。


From the Transactions doc, second paragraph:

The intention here is for the client to increment the total number of chat messages sent (ignore for a moment that there are better ways of implementing this).

What are some standard "better ways" of implementing this?

Specifically, I'm looking at trying to do things like retrieve the most recent 50 records. This requires that I start from the end of the list, so I need a way to determine what the last record is.

The options as I see them:

  • use a transaction to update a counter each time a record is added, use the counter value with setPriority() for ordering
  • forEach() the parent and read all records, do my own sorting/filtering at client
  • write server code to analyze Firebase tables and create indexed lists like "mostRecent Messages" and "totalNumberOfMessages"

Am I missing obvious choices?

解决方案

To view the last 50 records in a list, simply call "limit()" as shown:

var data = new Firebase(...);
data.limit(50).on(...);

Firebase elements are ordering first by priority, and if priorities match (or none is set), lexigraphically by name. The push() command automatically creates elements that are ordered chronologically, so if you're using push(), then no additional work is needed to use limit().

To count the elements in a list, I would suggest adding a "value" callback and then iterating through the snapshot (or doing the transaction approach we mention). The note in the documentation actually refers to some upcoming features we haven't released yet which will allow you to count elements without loading them first.

这篇关于Firebase更好地获取记录总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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