从数据库中获取所有子项 [英] Fetch all children from database

查看:96
本文介绍了从数据库中获取所有子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中是tableview,我想向我的客户显示所有子级客户.这是数据库结构:

In my app is tableview where i want to show all child of my customers. This is database structure:

以前在Customers下,我只有一个孩子,然后我知道当路径为usersDatabase/userID/Customers时如何向客户展示.但是在这一刻,我的路径是usersDatabase/userID/Customers/userSpecificName并且我的表视图显示空白单元格.为了正常工作,我必须在代码中添加些什么?

When previously under Customers I had only one child then I knew how to show customers when path was usersDatabase/userID/Customers. But in this moment my path is usersDatabase/userID/Customers/userSpecificName and my tableview show blank cell. What I must add in my code to properly working of code?

这是我从数据库导入数据时的代码:

This is code when I import data from Database:

let userID = Auth.auth().currentUser!.uid
    ref = Database.database().reference().child("usersDatabase").child(userID).child("Customers")

    ref.observe(DataEventType.value, with: { (snapshot) in
        if snapshot.childrenCount > 0 {
            self.services.removeAll()
            self.filteredServices.removeAll()
            for results in snapshot.children.allObjects as! [DataSnapshot] {
                let results = results.value as? [String: AnyObject]
                let name = results?["Name and surname"]
                let phone = results?["Phone"]
                let customerID = results?["ID"]

                let myCustomer = CustomerModel(name: name as? String, phone: phone as? String, customerID: customerID as? String)
                self.services.append(myCustomer)
                self.filteredServices.append(myCustomer)
            }
            self.tableView.reloadData()
        }
    })

我应该添加到表视图显示子项的 ref = Database.database().reference().child("usersDatabase").child(userID).child("Customers")行中的内容)新增的客户(本·史密斯和汤姆·克鲁斯)?

What I should add to line ref = Database.database().reference().child("usersDatabase").child(userID).child("Customers") that tableview show child of added Customers (Ben Smith and Tom Cruise)?

推荐答案

此答案类似于先前的答案

This answer is similar to a prior answer here

您要做的是将客户"中的每个子级视为DataSnapshot,然后可以访问这些子级.

What you want to do is to treat each child in Customers as a DataSnapshot, then the children can be accessed.

给出Firebase结构:

Given a Firebase structure:

usersDatabase
  uid_0
     Customers
        Ben Smith
           data: "Some data"
        Tom Cruise
           data: "Some data"
  uid_1
     Customers
        Leroy Jenkins
           data: "Some data"
  uid_2
     Customers
        De Kelly
           data: "Some data"
     etc

打印出每个用户及其客户的代码是

The code to print out each user and their customers is

let usersDatabaseRef = Database.database().reference().child("usersDatabase")
usersDatabaseRef.observe(.value, with: { snapshot in
    print("there are \(snapshot.childrenCount) users")
    for child in snapshot.children {
        let childSnap = child as! DataSnapshot
        print("user: \(childSnap.key)")
        let userCustomerSnap = childSnap.childSnapshot(forPath: "Customers")
        for customer in userCustomerSnap.children {
            let customerSnap = customer as! DataSnapshot
            print(" customer: \(customerSnap.key)")
        }
    }
})

和输出

there are 3 users
user: uid_0
 customer: Ben Smith
 customer: Tom Cruise
user: uid_1
 customer: Leroy Jenkins
user: uid_2
 customer: De Kelly
 customer: Leonard Nimoy
 customer: William Shatner

编辑:OP希望知道如何获取每个客户下的 data 节点,因此这是一个略有修改的版本,带有输出

Edit: the OP wanted to know how to get to the data node under each customer so here's a slightly modified version with output

let usersDatabaseRef = Database.database().reference().child("usersDatabase")
usersDatabaseRef.observe(.value, with: { snapshot in
    print("there are \(snapshot.childrenCount) users")
    for child in snapshot.children {
        let childSnap = child as! DataSnapshot
        print("user: \(childSnap.key)")
        let userCustomerSnap = childSnap.childSnapshot(forPath: "Customers")
        for customer in userCustomerSnap.children {
            let customerSnap = customer as! DataSnapshot
            let dict = customerSnap.value as! [String: Any]
            let value = dict["data"] as! String

            print(" customer: \(customerSnap.key)")
            print("    and their data is: \(value)")
        }
    }
})

和输出

there are 3 users
user: uid_0
 customer: Ben Smith
    and their data is: some data
 customer: Tom Cruise
    and their data is: some data
user: uid_1
 customer: Leroy Jenkins
    and their data is: some data
user: uid_2
 customer: De Kelly
    and their data is: some data
 customer: Leonard Nimoy
    and their data is: some data
 customer: William Shatner
    and their data is: some data

这篇关于从数据库中获取所有子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆