【问题标题】:Ignoring Firebase child忽略 Firebase 孩子
【发布时间】:2018-08-28 08:45:36
【问题描述】:

我怎样才能以这种方式查询这些数据:

我想忽略CurrentSubAdministrativeArea 孩子并迭代每个子孩子并找到正确的userKey

其实我用的是这段代码,那行不通:

self.ref.child("Ads").child("CurrentSubAdministrativeArea")
/*HERE I would like to ignore the childs*/
.queryOrdered(byChild: "userKey").queryEqual(toValue: uid).observeSingleEvent(of:.value, with: { (snapshot) in

--

{
  "Ads" : {
    "CurrentSubAdministrativeArea" : {
      "Mantova" : {
        "-L7ymBmmbHkNfhBRte9F" : {
          "cost" : 200,
          "date" : 1527256922000,
          "info" : "Test",
          "maxLimit" : 100,
          "minLimit" : 10,
          "personBadType" : [ "abitudinaria", "antipatica" ],
          "personGoodType" : [ "simpatica", "felice" ],
          "subAdministrativeArea" : "Mantova",
          "title" : "Mantova Test",
          "url" : "https://firebasestorage.googleapis.com/v0/b/team-34540.appspot.com/o/Mantova%20Test?alt=media&token=3a81ed1c-ecd6-4dc0-bd7c-45e093ce8188",
          "userKey" : "OsJRc98sqxPx70iqxFtoqerMzHH2",
          "via" : "viale dei test"
        }
      },
      "Milano" : {
        "-L6qywMC6nxi0fJNMHba" : {
          "cost" : 454,
          "date" : 1528298580000,
          "info" : "Di pollo",
          "maxLimit" : 100,
          "minLimit" : 10,
          "personBadType" : [ "abitudinaria", "antipatica" ],
          "personGoodType" : [ "simpatica", "felice" ],
          "subAdministrativeArea" : "Milano",
          "title" : "Pollo 2",
          "url" : "https://firebasestorage.googleapis.com/v0/b/team-34540.appspot.com/o/Pollo?alt=media&token=fc6a3ec8-5f9a-4347-bdad-2d9715af784d",
          "userKey" : "OsJRc98sqxPx70iqxFtoqerMzHH2",
          "via" : "viale test"
        }
      }
    }
  }
}

【问题讨论】:

    标签: ios json swift firebase firebase-realtime-database


    【解决方案1】:

    您可以以一种易于构建和执行查询的方式对数据进行非规范化。

    连同你已经拥有的数据结构,你将拥有另一个节点(即另一个数据结构),例如

    {
      "AdsByUsers" : {
        "OsJRc98sqxPx70iqxFtoqerMzHH2": {
          "Mantova",
          "Milano",
          ...
        },
        "abcde88qxPx70iqxFtoqerMzKh5": {
          "Firenze",
          ...
       }
    

    使用 NoSQL 数据库,您应该毫不犹豫地复制数据,这样您的查询就可以轻松快速地执行。

    【讨论】:

      猜你喜欢
      • 2011-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多