2017-02-09 92 views
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++; 
     } 
    } 
}]; 
+1

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 –

+1

Firebase最近发布了云端功能。看看这个[回答](http://stackoverflow.com/a/42713792/5861618)了解更多详情 –

回答

0

我提供SWIFTY的答案,但它很容易转换为对象 -

更改您的数据结构来此

{ 
    "data": { 
     "-Yuna99s993m": { 
      "created": 1484634400, 
      "status": "1" 
      "entry": "1" 
      "status_entry": "1_1" 
     }, 
     "-YUjns0909ks": { 
      "created": 1482612312, 
      "status": "0" 
      "entry": "2" 
      "status_entry": "0_2" 
     }, 
     "-Y8hj98nmswm": { 
      "created": 1481623400, 
      "status": "1" 
      "entry": "3" 
      "status_entry": "1_3" 
     }, 
     "-Y78903kjmsk": { 
      "created": 1485613233, 
      "status": "1" 
      "entry": "4" 
      "status_entry": "1_4" 
     }, 
     "-YJuikw9klkos": { 
      "created": 1489513532, 
      "status": 0 
      "entry": "5" 
      "status_entry": "0_5" 
     }, 

然后,查询以检索4之前的所有条目,状态为1

let dataRef = ref.child("data") 
let queryRef1 = dataRef.queryOrdered(byChild: "status_entry") 
let queryRef2 = queryRef1.queryStarting(atValue: "1_1") 
         .queryEnding(atValue: "1_3") 
queryRef2.observeSingleEvent(of: .value, with: { snapshot in 
    print(snapshot.childrenCount) 
}) 

运行时,该返回

2 

这意味着存在具有为1的状态条目4之前两个节点;这些节点是

"-Yuna99s993m": { 
    "created": 1484634400, 
    "status": "1" 
    "entry": "1" 
    "status_entry": "1_1" 
"-Y8hj98nmswm": { 
    "created": 1481623400, 
    "status": "1" 
    "entry": "3" 
    "status_entry": "1_3" 

*像“-Yuna99s993m”节点名称与childByAutoId创建 - 它通常是从它们所包含的子数据撇清节点键名的最佳实践。

这里的思考过程是,通过将两个变量(条目号和状态)组合到一个变量status_entry中,我们限制了startingAt和endingAt返回的内容。因此1_x将消除所有0_状态,并且通过指定从x_1到x_3的条目来进一步限制返回的节点。

诀窍是在x_4之前获取节点,因为它将成为.endingAt。如果它总是x_3,那么很简单。