从具有多个子项的Firebase数据库中检索

时间:2016-12-06 12:45:32

标签: swift firebase swift3 firebase-realtime-database

我一直在使用Firebase一段时间没有问题,但是最近我有一个包含多个子节点的数据库结构,并且我在我的代码上读取它时遇到了问题。 这是我的Firebase数据库结构:

Employee

我的问题是阅读内部信息,例如500zNLsK / content / 1 / parahraph / text中的文本或500zNLsK / content / 2 / quote / quoteText中的引用文本

我到目前为止:

{
  "news" : {
    "50OzNLsK" : {
      "category" : "Anuncio",
      "content" : [ null, {
        "paragraph" : {
          "text" : "Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
        }
      }, {
        "quote" : {
          "image" : "http://www.magonicolas.cl",
          "quoteText" : "textoDelQuote"
        }
      } ],
      "date" : "Hoy",
      "imgURL" : "https://firebasestorage.googleapis.com/v0/b/rinnofeed.appspot.com/o/images%2Ftanta.jpg?alt=media&token=60f18a95-9919-4d81-ab1c-9976b71590fc",
      "likes" : 12,
      "summary" : "Este es el resumen de la noticia",
      "title" : "Mi NUeva noticia",
      "videoURL" : "https://youtu.be/S0YjUc7K3cw"
    }
  },
  "users" : {
    "tfeMODxXUnVvhvzntm77pKKtyfz2" : {
      "email" : "magonicolas@gmail.com",
      "store" : "Rinno"
    },
    "tpjn4feUuiTJmsd9lslHTREG1iE2" : {
      "email" : "nicolas.palacios@rinno.la",
      "store" : "Rinno"
    }
  }
}

我无法显示内部内容,它给出了null。 帮助我很感激:D

1 个答案:

答案 0 :(得分:2)

您需要对结构进行一些更改才能使其工作流畅。这是一个让您走向正确方向的示例结构:

  "news"
    "topic_1"
      "category" : "topic 1 category"
      "content"
        "content_1"
          "paragraph"
            "text" : "topic 1 content 1 text"
        "content_2"
          "paragraph"
            "text" : "topic 1 content 2 text"
      "date" : "some date",
      "likes" : 12

    "topic_2"
      "category" : "topic 2 category",
      "content"
        "content_1"
          "paragraph"
            "text" : "topic 2 cotent 1 text"
        "content_2"
          "paragraph"
            "text" : "topic 2 content 2 text"
      "date" : "another date",
      "likes" : 425

这是用于访问“内部”大多数数据的注释代码

  newsRef.observe(.value, with: { snapshot in

    if ( snapshot!.value is NSNull ) {
        print("not found")
    } else {

        for child in (snapshot?.children)! {

            //snapshots are each node withing news: topic_1, topic_2
            let snap = child as! FDataSnapshot 

            //get each nodes data as a Dictionary
            let dict = snap.value as! [String: AnyObject] 

            //we can now access each key:value pair within the Dictionary
            let cat = dict["category"]
            print("category = \(cat!)")

            let likes = dict["likes"]
            print("  likes = \(likes!)")

            //the content node is a key: value pair with 'content' being the key and
            //  the value being a dictionary of other key: value pairs
            let contentDict = dict["content"] as! [String: AnyObject]

            //notice that the content_1 key: value pair has a key of content_1 
            //   and a value of yet another Dictionary
            let itemNum1Dict = contentDict["content_1"] as! [String: AnyObject]

            //new we need to get the value of content_1, which is
            //  'paragraph' and another key: value pair
            let paragraphDict = itemNum1Dict["paragraph"] as! [String: AnyObject]

            //almost there.. paragraphDict has a key: value pair of
            //   text: 'topic 1 content 1 text'
            let theText = paragraphDict["text"] as! String
            print("  text = \(theText)")

        }
    }
})

一些注意事项:

1)请不要在Firebase中使用用户数组。他们是邪恶的,会给你带来极大的悲伤。您的节点名称应该是描述节点的内容(如“news”)和使用childByAutoId创建的子值。即使在“内容”节点中,我也使用了content_1和content_2,但也应该使用childByAutoId创建

2)如果你在我的例子中迭代节点,节点内的结构应该保持一致。你实际上可以解决这个问题,但对于查询等,你总是更好地保持一致。

3)Denormalizing is Normal。这种结构有效,但你可以获得一些优势,使整个结构更浅。