Firebase 中的记录总数(我什么时候数完?)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11618032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Total number of records in Firebase (when am I done counting?)
提问by Kato
Counting records in a table is obviously a manual effort until you guys get some of that spiffy new functionality already in the works ;)
计算表中的记录显然是一项手动工作,直到你们得到一些已经在工作中的漂亮的新功能;)
However, I'm stuck on even using a manual run with .on('value', ...) to fetch a count:
但是,我什至坚持使用 .on('value', ...) 手动运行来获取计数:
var table = new Firebase('http://beta.firebase.com/user/tablename');
var count = 0;
table.on('child_added', function(snapshot) {
count++;
// how do I know if this is the last child? i.e. the count is complete?
});
// when is it okay to use count?
I foresee the same issues with any sort of pagination and I feel like I'm being a bit blockheaded about this. What am I missing?
我预见到任何类型的分页都会出现同样的问题,而且我觉得我对此有点不知所措。我错过了什么?
Is this fundamentally the wrong pattern for, say, getting the number of messages a user has in his/her queue?
这是否从根本上是错误的模式,例如,获取用户在他/她的队列中的消息数量?
回答by Michael Lehenbauer
The child_added event has no notion of "done", since children can continue to be added over time. If you want to get a count of the children right now, you can use 'value' to get the current snapshot from that location and then count the children. For example:
child_ added 事件没有“完成”的概念,因为随着时间的推移可以继续添加子项。如果你想获得孩子的数量,现在,你可以用“价值”从那个位置的当前快照,再算上孩子。例如:
table.once('value', function(snapshot) {
var count = 0;
snapshot.forEach(function(childSnapshot) {
count++;
});
// do something with count.
});
Alternatively, if you want the count to continually update, you can use .on() instead of .once() above. But this isn't ideal performance-wise, since you'll be counting all of the children every time. Fortunately, you can use a combination of 'child_added' and 'value' to efficiently keep a count:
或者,如果您希望计数不断更新,您可以使用 .on() 而不是上面的 .once() 。但这在性能方面并不是理想的,因为您每次都会计算所有的孩子。幸运的是,您可以使用 'child_ added' 和 'value' 的组合来有效地保持计数:
var count = 0;
table.on('child_added', function(snapshot) {
count++;
// how do I know if this is the last child? i.e. the count is complete?
});
table.on('value', function(snapshot) {
// do something with count.
});
This works since 'value' will fire once after the "initial" data is complete and then again whenever the data changes, so you'll get a continually updating correct count. Though you'll need to handle child_removed as well if you expect children to be removed.
这是有效的,因为 'value' 将在“初始”数据完成后触发一次,然后在数据更改时再次触发,因此您将获得不断更新的正确计数。尽管如果您希望移除孩子,您还需要处理 child_removed 。
回答by Andrew Lee
With "child_added" there is no way to know when you've received the "last" item. If you're looking to count all of the children that existed at a particular point in time, I'd suggest using the "value" event as follows:
使用“child_ added”,无法知道您何时收到“最后一个”项目。如果您想计算在特定时间点存在的所有孩子,我建议使用“value”事件,如下所示:
var table = new Firebase('http://beta.firebase.com/user/tablename');
table.on('value', function(snapshot) {
var count = 0;
snapshot.forEach(function() {
count++;
});
//count is now safe to use.
});
回答by killjoy
Until FB implements count as a returned function, I guess the best way to not have client side loop through any returned records is to fetch it the REST way.
在 FB 实现 count 作为返回函数之前,我想不让客户端循环遍历任何返回记录的最佳方法是以 REST 方式获取它。
ref: What's the best RESTful method to return total number of items in an object?

