【问题标题】:How do I use Firebase Database to display multiple routes/childs in Table View using Xcode/Swift如何使用 Firebase 数据库在 Xcode/Swift 的表视图中显示多个路由/子节点
【发布时间】:2019-07-30 09:15:04
【问题描述】:

这是我的 Firebase 数据库:

这是我的代码:

class AdminSearchViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    var ref: DatabaseReference!
    var jobList = [jobModel2]()

    @IBOutlet weak var tlb2: UITableView!

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! ViewController2TableViewCell
        let job: jobModel2
        job = jobList[indexPath.row]
        cell.ship.text = job.consignee
        cell.reference.text = job.reference
        return cell
    }

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

    override func viewDidLoad() {
        super.viewDidLoad()
        if FirebaseApp.app() == nil {
            FirebaseApp.configure()
        }
        Database.database().reference().child("jobs").observe(DataEventType.value) { (snapshot) in
            if snapshot.childrenCount>0 {
                self.jobList.removeAll()
                for jobs in snapshot.children.allObjects as! [DataSnapshot]{
                    let jobObject = jobs.value as? [String: AnyObject]
                    let jobConsignee = jobObject?["consignee"]
                    let jobReference = jobObject?["reference"]
                    let job = jobModel2( consignee: jobConsignee as! String?,
                                         reference: jobReference as! String?
                    )
                    self.jobList.append(job)
                }
                self.tlb2.reloadData()
            }
        }
    } 
}

我面临的问题是它们在我的 TableView 中没有出现,我认为这与 .childs 有关,因为我认为我需要将其从“工作”更改为某些东西,但我想不通出去。

我想显示来自所有 UID 的所有引用的列表,而不仅仅是一个。

打印中显示的代码:

func readJobs() {


    let ref = Database.database().reference()

   let ref1 = ref.child("jobs").queryOrderedByKey()


        ref1.observeSingleEvent(of: .value, with: { snapshot in
            let allUsersAndJobs = snapshot.children.allObjects as! [DataSnapshot]
            for user in allUsersAndJobs {
                let uid = user.key
                print("user id: \(uid)")
                let thisUsersJobs = user.children.allObjects as! [DataSnapshot]
                for job in thisUsersJobs {
                    let jobKey = job.key
                    let consignee = job.childSnapshot(forPath: "consignee").value as! String
                    print(" job #: \(jobKey)")
                    print("   consignee: \(consignee)")
                }
            }
        })
    }

【问题讨论】:

  • 最好将代码和结构包含为文本,而不是链接和图像。要获取您的 Firebase 结构,请使用 Firebase 控制台->导出 JSON 并复制粘贴您的结构的 sn-p。见images and links are evil

标签: ios swift firebase firebase-realtime-database


【解决方案1】:

所以这行不通 - firebase 结构与您尝试读取的内容不匹配。

你的结构是

jobs
   uid_0
      job_key_0
          ...
      job_key_1
          ...
   uid_1
      job_key_0
          ...

您正在读取包含直接子代的作业节点,但您没有读取子代的子代。

uid_0
uid_1
etc

因此,Firebase 结构比您正在阅读的内容更深,因此您需要在代码中再往下阅读一层才能到达作业子节点。

假设 5hs... 和 7py... 是用户 id,这里是读取每个节点、打印用户 uid 和他们的作业的代码

func readJobs() {
    let ref = self.ref.child("jobs")
    ref.observeSingleEvent(of: .value, with: { snapshot in
        let allUsersAndJobs = snapshot.children.allObjects as! [DataSnapshot]
        for user in allUsersAndJobs {
            let uid = user.key
            print("user id: \(uid)")
            let thisUsersJobs = user.children.allObjects as! [DataSnapshot]
            for job in thisUsersJobs {
                let jobKey = job.key
                let consignee = job.childSnapshot(forPath: "consignee").value as! String
                print(" job #: \(jobKey)")
                print("   consignee: \(consignee)")
            }
        }
    })
}

和输出

user id: 5HS
 job #: job_0
   consignee: Anni
 job #: job_1
   consignee: Ralph
user id: 7py
 job #: job_0
   consignee: Larry
 job #: job_1
   consignee: Bill

【讨论】:

  • 不,5HS...,7py... 是使用 firebase 生成的用户 UID。不是一个类别,因为它不会打印任何内容,这有什么不同吗?
  • @Wmwm19 啊,好吧,这是有道理的。不,它没有任何区别——它仍然是一个父节点。每一个都需要读入,然后遍历子作业节点。我已经更新了代码并调整了变量名称,使其与您正在做的事情相匹配。我复制了您的结构并运行了该代码,您可以看到输出就是您所要求的。
  • 好吧,这很好,但是现在出现线程 1 的错误:致命错误:Unexpectedly found nil while unwrapping an Optional value 。不太清楚为什么?有什么想法吗?
  • 在这一行:let ref = self.ref.child("jobs")
  • 另外,我如何把它放在表格视图中,而不是打印这个?这可能吗?
猜你喜欢
  • 2017-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-22
  • 2023-01-16
  • 2017-11-18
  • 1970-01-01
相关资源
最近更新 更多