【问题标题】:Get children of child nodes from firebase (swift 3)从firebase获取子节点的子节点(swift 3)
【发布时间】:2017-08-24 21:46:29
【问题描述】:

如何从 firebase 获取 child 节点的 children?现在我使用此代码获取 child,但无法从第一个和第二个 childrens 获取 Images

    var images = [String]()
    var images2 = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        nameAddressRef = Database.database().reference(withPath: "Студии")

        loadImages()
    }
    func loadImages() {
        nameAddressRef.observe(DataEventType.value, with: { (snapshot) in
            var newImage = [String]()
            var newImage1 = [String]()

            for child in snapshot.children {
                let snap = child as! DataSnapshot
                let imageSnap = snap.childSnapshot(forPath: "Images")
                let dict = imageSnap.value as! [String: AnyObject]
                let url1 = dict["image1"] as! String
                let url2 = dict["image2"] as! String

                newImage.append(url1)
                newImage1.append(url2)

            }
            self.images = newImage
            self.images2 = newImage1
        })
    }
}
extension ViewController: UITableViewDataSource, UITableViewDelegate {

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CategoryRow

        cell.nameLabel.text = nameAddress[indexPath.section].name

        let array = [images[indexPath.section],
                      images2[indexPath.section]]
        cell.images = array.filter({ !($0.isEmpty) })

        return cell
    }
}

我知道我在我的功能中遗漏了一些东西,但我无法理解我遗漏了什么。 还有我的 firebase 结构:

{
  "Студии" : {
    "Дубаи" : {
      "1" : {
        "Images" : {
          "image1" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/%D0%A1%D0%BA%D1%80%D0%B8%D0%BD%D1%88%D0%BE%D1%82%202017-08-09%2011.22.04.png?alt=media&token=e3e2b8ed-6ce3-4a87-91ae-1194496bf1f1",
          "name" : "qwerty"
        }
      },
      "2" : {
        "Images" : {
          "image1" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/%D0%A1%D0%BA%D1%80%D0%B8%D0%BD%D1%88%D0%BE%D1%82%202017-08-09%2011.22.04.png?alt=media&token=e3e2b8ed-6ce3-4a87-91ae-1194496bf1f1",
          "name" : "qwerty2"
        }
      },
      "Images" : {
        "image1" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/%D0%A1%D0%BA%D1%80%D0%B8%D0%BD%D1%88%D0%BE%D1%82%202017-08-09%2011.22.04.png?alt=media&token=e3e2b8ed-6ce3-4a87-91ae-1194496bf1f1"
      },
      "address" : "Тутушка",
      "name" : "Дубаи"
    }
  }
}

感谢您的帮助!

【问题讨论】:

  • 你能重复一下吗:let imageSnap = snap.childSnapshot(forPath: "Images")1 -> Images

标签: ios swift firebase firebase-realtime-database


【解决方案1】:

试试这个:-

   func loadImages (){
    Database.database().reference(withPath: 
   "Студии").child("Дубаи").observe(.value, with: { (snapShot) in
        if snapShot.exists() {
            let array:NSArray = snapShot.children.allObjects as NSArray

            for child in array {
                let snap = child as! DataSnapshot
                if snap.value is NSDictionary {
                    let data:NSDictionary = snap.value as! NSDictionary
                    if let dict = data.value(forKey: "Images") {
                        let dictImage:NSDictionary = dict as! 
        NSDictionary
                        if let image  = dictImage["image1"] {
                            print(image)
                        }
                    }
                }

               // newImage1.append(url2)

            }
        }
    })
}

【讨论】:

  • 谢谢,它正在工作。但是,如果我想从多个路径中获取,我该怎么做?例如,我将有“Дубаи”、“Лондон”和“Москва”。
【解决方案2】:

Студии 下没有Images,所以循环不匹配。

您需要在 JSON 中收听低一级:

nameAddressRef = Database.database().reference(withPath: "Студии/Дубаи")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-03
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    相关资源
    最近更新 更多