【问题标题】:Issue retrieving firebase child node swift快速检索firebase子节点的问题
【发布时间】:2017-09-28 14:57:22
【问题描述】:

Xcode 9 - 斯威夫特 4

没有在 Firebase 数据上设置权限 - 读/写每个人

我将 json 数据导入 firebase,我的数据看起来像这样..

我正在尝试获取 FireBase 数据库中列出的作业的标题,将标题列表放在一个数组中并放入一个 tableView 中,它不会返回任何内容 我的快速代码看起来像这样..

import UIKit
import FirebaseDatabase


class PostViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var jobPostsTableView: UITableView!

    var ref: DatabaseReference?
    var databaseHandle: DatabaseHandle = 0


    var searchJSONQuery : String = ""

    var jobsData = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        jobPostsTableView.delegate = self
        jobPostsTableView.dataSource = self

        //Set the Firebase Reference
        ref = Database.database().reference()



    // Retreive the posts and listen for changes
        databaseHandle = (ref?.child("positions/title").observe(.childAdded, with: { (snapshot) in
            //Code to execute when a child is added under "positions"
            //Take the value from the snapshot and add it to the jobsData array

            let list = snapshot.value as? String

            if let actualList = list {
                self.jobsData.append(actualList)
                self.jobPostsTableView.reloadData()
            }

        }))!


}
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return jobsData.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell")
        cell?.textLabel?.text = jobsData[indexPath.row]
        return cell!
    }


}

【问题讨论】:

  • 有什么问题?
  • @ibrahim Akar 我已经编辑了我的答案,试试吧
  • 非常感谢@matias 的帮助 ..它不仅把我带到了正确的地方,我实际上学到了很多东西 .. 缺少一件事 .. 孩子必须被铸造对于 snapshot.children.allObjects 中的孩子! [数据快照]{

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


【解决方案1】:

当使用child() 时,您当时只能在树下走一层。因为您有很多职位,所以您不能简单地使用child("title") 访问标题。

调用observeSingleEvent 时,您正在寻找您在数据库参考中声明的键的

通过下面写的方式,您可以获得“位置”键下所有值的快照。因此,您使用 for 循环来访问每个对象的“标题”值。

您应该将其编写为一个单独的函数并从 viewDidLoad() 调用它,而不是在 viewDidLoad 本身中编写 firebase 代码。

func retrieveJobTitles(){

        let positionRef = ref.child("positions")

        positionsRef.observeSingleEvent(of: .value, with: { (snapshot) in

            // Iterate through all of your positions
            for child in snapshot.children.allObjects as! [DataSnapshot] {

               let position = child as! DataSnapshot

               let positionsInfo = position.value as! [String: Any]

               let jobTitle = positionsInfo["title"] as! String

               if jobTitle != nil {
                  self.jobsData.append(jobTitle)
               }
            }

            self.jobPostsTableView.reloadData()
        })
    }
}

【讨论】:

  • 即使你说的是对的,但实际上你并没有告诉他他做错了什么。实际问题是他正在使用的“DataEventType”。他应该使用“.value”,而不是使用“.childAdded”。
  • @MassimilianoDelMaestro 你说得对。我将编辑我的答案。
  • 我使用了 matiastofteby 建议的函数 - 谢谢 // 遍历你在 snapshot.children 中的所有孩子位置 { 我得到错误:不可变值 'child' 从未使用过;考虑用“_”替换或删除它 - 没有返回数据
  • 抱歉我已经更新了。还将userInfo 更改为positionsInfo
  • 我现在在这一行出现一个错误.. 让 positionInfo = child.value as! [字符串:Any] 错误:“Any”类型的值没有成员“值”将“Any”转换为“AnyObject”或使用“as!”强制向下转换为更具体的类型以访问成员
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多