0
火力地堡数据:火力地堡计数儿童
{
"data": {
"entry-1": {
"created": 1484634400,
"status": 1
},
"entry-2": {
"created": 1482612312,
"status": 0
},
"entry-3": {
"created": 1481623400,
"status": 1
},
"entry-4": {
"created": 1485613233,
"status": 1
},
"entry-5": {
"created": 1489513532,
"status": 0
},
"entry-6": {
"created": 1483123532,
"status": 1
},
"entry-7": {
"created": 1481282376,
"status": 1
},
"entry-8": {
"created": 1432321336,
"status": 1
},
"entry-9": {
"created": 1464282376,
"status": 0
}
}
}
我想看看有多少活跃条目(status
= 1)entry-4
之前创建的,并保持实时更新计数。
今天我聆听数据库中的每一个变化,但它消耗了大量不必要的数据。有一个更好的方法吗?
代码:
FIRDatabaseQuery *query = [self.firebase child:@"data"];
[query observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
int count = 0;
for (FIRDataSnapshot *child in snapshot.children) {
if (child.value[@"status"] == 1 && child.value[@"created"] < 1485613233) {
count++;
}
}
}];
Firebase数据库中没有计数运算符。为了防止必须下载所有项目以获得计数,您可以保留一个单独的'count'节点 - 然后您可以更新事务。请参阅http://stackoverflow.com/questions/15148803/in-firebase-is-there-a-way-to-get-the-number-of-children-of-a-node-without-load/15149311#15149311 –
Firebase最近发布了云端功能。看看这个[回答](http://stackoverflow.com/a/42713792/5861618)了解更多详情 –