簡體   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