从Firebase数据库中获取嵌套子级

时间:2019-03-10 00:50:18

标签: ios swift firebase firebase-realtime-database

我正在尝试使用Firebase数据库中的值填充表视图。我能够在数据库的根目录上使用queryOrdered来获取所需的子级,如下所示:

func oneTimeInit() {
        databaseRef?.queryOrdered(byChild: "Seasons").observe(.value, with:
            { snapshot in
                for item in snapshot.children {
                    self.seasons.append(Season( snapshot: item as! DataSnapshot))
                }
                self.seasonsTableView.reloadData()
        })
    }

但是现在我试图在数据库中获取下一个节点的子节点。我试图通过在根目录的子目录上调用queryOrdered来做到这一点,但是我没有得到任何数据。这是我的代码:

func oneTimeInit() {
        databaseRef?.child("Seasons").queryOrdered(byChild: season.name).observe(.value, with:
            { snapshot in
                for item in snapshot.children {
                    print("ITEM VALUE: \(item)")
                    self.races.append(Race(snapshot: item as! DataSnapshot))
                }
                self.racesTableView.reloadData()
        })
    }

数据库包含最高级别的越野赛,每个赛季都包含越野赛,每个赛季都包含结果。我可以填充“季节”表,但不能填充“种族”表,等等。

这里是我数据库的Json结构:

{
  "Seasons" : {
    "XC 2018" : {
      "NCAA DI West Regional" : {
        "Men" : {
          "Results" : {
            "Aaron BRUMBAUGH" : {
              "finishTime" : "31:59.0",
              "lapTimes" : [ "5:05.1", "10:43.3", "11:07.1", "5:03.5" ],
              "name" : "Aaron BRUMBAUGH",
              "position" : "118",
              "splitTimes" : [ "5:05.1", "15:48.4", "26:55.5", "31:59.0" ],
              "team" : "Santa Clara",
              "year" : "SR"
            },
            "Addison DEHAVEN" : {
              "finishTime" : "29:47.8",
              "lapTimes" : [ "4:58.2", "10:14.0", "10:04.1", "4:31.5" ],
              "name" : "Addison DEHAVEN",
              "position" : "7",
              "splitTimes" : [ "4:58.2", "15:12.2", "25:16.3", "29:47.8" ],
              "team" : "Boise State",
              "year" : "SR"
            },
            "Ahmed MUHUMED" : {
              "finishTime" : "30:08.1",
              "lapTimes" : [ "4:58.6", "10:14.6", "10:08.5", "4:46.4" ],
              "name" : "Ahmed MUHUMED",
              "position" : "29",
              "splitTimes" : [ "4:58.6", "15:13.2", "25:21.7", "30:08.1" ],
              "team" : "Boise State",
              "year" : "SO"
            },

截至目前,只有一个“季节”(XC 2018)和一个“种族”(NCAA D1西部地区)。我正在尝试引用“ Season” XC 2018下的所有“ Races”。

变量season.name将包含值“ XC 2018”。第二次我要尝试看一下“ XC 2018”的孩子们。

0 个答案:

没有答案