Firebase - 下载特定的孩子(Swift 3)

时间:2017-07-08 19:05:34

标签: ios firebase swift3 firebase-realtime-database

我附上了我的JSON树的图片

{
  "-Ko4cg3ik5OFsnLTI4q4" : {
    "FacebookPage" : {
      "id" : "187736205061418",
      "page" : "partyunlimitedtoronto"
    },
    "MapView" : {
      "Edate" : 523940400,
      "Sdate" : 523929600,
      "intersted" : 3843,
      "lat" : 45.510772273476,
      "lng" : -73.563586483671,
      "type" : "MUSIC",
      "userID" : 1373802105986614,
      "visibility" : "Public"
    },
    "address" : "Théâtre Corona",
    "bio" : "evenko & Greenland Productions présentent\t\nMogwai\navec/with Xander Harris \nPortes: 19h00 - Spectacle: 20h00\n\nMise en vente: 24 mars à 10h\n\n37$ à 42$\n\nPrix lors de l’achat à la billetterie / Des frais de manutention peuvent s’appliquer lors de l’achat en ligne.\n\n.....................................................\n\nDoors: 7:00 pm - Show: 8:00 pm\n\nOn Sale: March 24 at 10am\n\n$37 to $42\n\nPrices for box office purchase / Handling fees may apply for online purchase.",
    "name" : "Mogwai // Montréal",
    "pic" : "https://scontent.xx.fbcdn.net/v/t1.0-9/17361785_10155183713782422_6829705267219994059_n.jpg?oh=150f6f53c74233b70b7f1b372c1db1e4&oe=59CF07EE",
    "site" : "http://www.evenko.ca/fr/evenements/12392/mogwai/theatre-corona/12-06-2017",
    "status" : "normal"
  },
  "-Ko4cgP2rXJCIRBODWpm" : {
    "FacebookPage" : {
      "id" : "620998508097068",
      "page" : "partyunlimitedtoronto"
    },
    "MapView" : {
      "Edate" : 43223454,
      "Sdate" : 65837495,
      "interested" : 2355,
      "lat" : 45.3846583,
      "lng" : -74.4736583,
      "type" : "Party",
      "userID" : 1373802105986614,
      "visibility " : "Public"
    },
    "address" : "Théâtre Corona",
    "bio" : "Greenland Productions & evenko présentent\t\t\t\t\t\t\nNazareth\t\t\t\navec/with Les Deuxluxes\t\n\nPortes: 19h00 - Spectacle: 20h00\n\nMise en vente: 14 avril à midi \n\n44,50$ à 48$\n\nPrix lors de l’achat à la billetterie / Des frais de manutention peuvent s’appliquer lors de l’achat en ligne.\n\n.....................................................\n\nDoors: 7:00 pm - Show: 8:00 pm\n\nOn Sale: April 14 at noon\n\n$44,50 to $48\n\nPrices for box office purchase / Handling fees may apply for online purchase.",
    "name" : "Nazareth // Montréal",
    "pic" : "https://scontent.xx.fbcdn.net/v/t1.0-9/17759677_10155255160737422_1735715699829358715_n.jpg?oh=1d6d1c250c2c6b38c6ef0de4483bd0a9&oe=5A0C4879",
    "site" : "http://www.evenko.ca/fr/evenements/12489/nazareth/theatre-corona/08-03-2017",
    "status" : "normal"
  }
}

所以我尝试在运行程序时只在每个孩子中下载“MapView”节点,但在这种情况下,我不知道“MapView”父级的名称。有没有办法只下载像这种情况下的特定孩子。否则,我在想是否应该使用这种结构风格。

{
  "EventView" : {
    "-Ko4cg3ik5OFsnLTI4q4" : {
      "FacebookPage" : {
        "id" : "187736205061418",
        "page" : "partyunlimitedtoronto"
      },
      "address" : "Théâtre Corona",
      "bio" : "evenko & Greenland Productions présentent\t\nMogwai\navec/with Xander Harris \nPortes: 19h00 - Spectacle: 20h00\n\nMise en vente: 24 mars à 10h\n\n37$ à 42$\n\nPrix lors de l’achat à la billetterie / Des frais de manutention peuvent s’appliquer lors de l’achat en ligne.\n\n.....................................................\n\nDoors: 7:00 pm - Show: 8:00 pm\n\nOn Sale: March 24 at 10am\n\n$37 to $42\n\nPrices for box office purchase / Handling fees may apply for online purchase.",
      "name" : "Mogwai // Montréal",
      "pic" : "https://scontent.xx.fbcdn.net/v/t1.0-9/17361785_10155183713782422_6829705267219994059_n.jpg?oh=150f6f53c74233b70b7f1b372c1db1e4&oe=59CF07EE",
      "site" : "http://www.evenko.ca/fr/evenements/12392/mogwai/theatre-corona/12-06-2017",
      "status" : "normal"
    },
    "-Ko4cgP2rXJCIRBODWpm" : {
      "FacebookPage" : {
        "id" : "620998508097068",
        "page" : "partyunlimitedtoronto"
      },
      "address" : "Théâtre Corona",
      "bio" : "Greenland Productions & evenko présentent\t\t\t\t\t\t\nNazareth\t\t\t\navec/with Les Deuxluxes\t\n\nPortes: 19h00 - Spectacle: 20h00\n\nMise en vente: 14 avril à midi \n\n44,50$ à 48$\n\nPrix lors de l’achat à la billetterie / Des frais de manutention peuvent s’appliquer lors de l’achat en ligne.\n\n.....................................................\n\nDoors: 7:00 pm - Show: 8:00 pm\n\nOn Sale: April 14 at noon\n\n$44,50 to $48\n\nPrices for box office purchase / Handling fees may apply for online purchase.",
      "name" : "Nazareth // Montréal",
      "pic" : "https://scontent.xx.fbcdn.net/v/t1.0-9/17759677_10155255160737422_1735715699829358715_n.jpg?oh=1d6d1c250c2c6b38c6ef0de4483bd0a9&oe=5A0C4879",
      "site" : "http://www.evenko.ca/fr/evenements/12489/nazareth/theatre-corona/08-03-2017",
      "status" : "normal"
    }
  },
  "MapView" : {
    "-Ko4cg3ik5OFsnLTI4q4" : {
      "Edate" : 534312000,
      "Sdate" : 534301200,
      "intersted" : 652,
      "lat" : 45.4827983,
      "lng" : -73.575107687761,
      "type" : "MUSIC",
      "userID" : "1373802105986614",
      "visibility" : "Public"
    },
    "-Ko4cgP2rXJCIRBODWpm" : {
      "Edate" : 523508400,
      "Sdate" : 523497600,
      "intersted" : 382,
      "lat" : 45.4827983,
      "lng" : -73.575107687761,
      "type" : "MUSIC",
      "userID" : "1373802105986614",
      "visibility" : "Public"
    }
  }
}

在这种情况下,我会将信息放在一个单独的子节点中,但在EventView和MapView中都有父节点具有相同的名称。

1 个答案:

答案 0 :(得分:0)

对于第一个结构

这个结构肯定会让我们的查询下载整个EventView节点

var AllMapViews = []; //<-- array of all mapview objects

var path = "/EventView";
firebase.database().ref(path)
                .once("value")
                .then(eventViews => {

                    eventViews.forEach(thisevent => {

                       AllMapViews.push(thisevent.val().MapView);

                    })
                });
  

因此上述结果将如下所示

AllMapViews = [
{ "EventDate":343,"Sdate" : 65837495 ..},
{ "EventDate":343,"Sdate" : 65837495 ..},
{ "EventDate":343,"Sdate" : 65837495 ..},
...
]

对于第二个结构(我的选择)

此结构将允许我们的查询仅为所有EventViews下载MapView。因此,使其成为您实施中的首选结构。虽然它需要您经常监视主EventView节点以了解主事件何时被删除,以便您也可以删除它的相应MapView。

var AllMapViews = []; //<-- array of all mapview objects

    var path = "/MapViews";
    firebase.database().ref(path)
                    .once("value")
                    .then(mapViews => {

                        mapViews.forEach(map => {

                           AllMapViews.push(map.val());

                        })
                    });
  

因此上述结果将如下所示

AllMapViews = [
{ "EventDate":343,"Sdate" : 65837495 ..},
{ "EventDate":343,"Sdate" : 65837495 ..},
{ "EventDate":343,"Sdate" : 65837495 ..},
...
]