繁体   English   中英

火力计数儿童

[英]Firebase count Children

Firebase数据:

{
    "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
        }
    }
}

我正在尝试计算在entry-4之前创建了多少个活动条目( status = 1),并使计数保持实时更新。

今天,我聆听数据库中的所有更改,但是它正在消耗大量不必要的数据。 有一个更好的方法吗?


码:

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++;
        }
    }
}];

我提供的是Swifty答案,但可以轻松转换为Obj-C

将您的数据结构更改为此

{
    "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"
        },

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

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

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

"-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,则很简单。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM