访问Firebase数据库中的多个节点 [英] Access Multiple Nodes in Firebase Database

查看:65
本文介绍了访问Firebase数据库中的多个节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难从我的Firebase数据库中的一个节点中提取所有数据。

I am having a hard time pulling all my data from one of my nodes in my firebase database.

以下是该节点在Firebase中的外观:

Here is how the node looks like in Firebase:

Considerations
     -MEdUNZwVrsDW3dTrE6N
        Company Description: 
        Company Image: 
        Company Name: 
        Decision Date: 
        Start Date: 
        Users
           B2Z4DlZ8RucvEQhz2NSUkquqc5P2
              Compensation: 
              PostNumber: 
              StoryNumber:

在用户下,将会有多个人的补偿,职位号和故事编号具有不同的值。我让每个用户都有一个称为用户注意事项的节点。该标签会标记每个用户附加的对价的唯一ID,并将其放在其UID下,并在其旁边将1标记为值。我正在尝试访问节点中每个特定用户的信息以及其他信息。这是我用来调用信息的代码以及用来捕获信息的结构:

Under users there are going to be multiple people with different values for the compensation, post number, and storynumber. I have each user having a node called "user-considerations" that tags the unique id of the consideration each user is attached on and places it under their UID and tags a 1 next to it as the value. I am trying to access each specific user's info along with the other info in the node. Here is my code that I am using to call the information along with the struct I a using to capture the information:

STRUCT:

import UIKit

class ConsiderationInfo: NSObject {
    var companyName: String?
    var companyImage: String?
    var companyDescription: String?
    var decisionDate: String?
    var startDate: String?
    var compensation: String?
    var postNumber: String?
    var storyNumber: String?
}

观察信息的代码:

 func updateConsiderationsArray() {
    
    let uid = Auth.auth().currentUser?.uid
    let ref = Database.database().reference().child("user-considerations").child(uid!)
            
            ref.observe(.childAdded, with: { (snapshot) in
            
                    let considerationId = snapshot.key
                    let considerationReference = Database.database().reference().child("Considerations").child(considerationId)
            
                    considerationReference.observe(.value, with: { (snapshot) in

                    if let dictionary = snapshot.value as? [String: AnyObject] {
                    let considerationInfo = ConsiderationInfo()
                    //self.setValuesForKeys(dictionary)
                    considerationInfo.companyName = dictionary["Company Name"] as? String
                    considerationInfo.companyImage = dictionary["Company Image"] as? String
                    considerationInfo.companyDescription = dictionary["Company Description"] as? String
                    considerationInfo.decisionDate = dictionary["Decision Date"] as? String
                    considerationInfo.startDate = dictionary["Start Date"] as? String
                
                    self.considerationsInfo.append(considerationInfo)
                    self.considerationName.append(considerationInfo.companyName!)
                    self.filteredConsiderations.append(considerationInfo.companyName!)
                        
                        self.considerationCollectionView.reloadData()
                }
            }, withCancel: nil)
        })
    }

我正在尝试访问用户特定节点下的信息,即特定用户的补偿职位编号和故事编号。我不知道如何访问所有这些内容以附加该结构。

I am trying to access the information under the user specific node, i.e. the specific user's compensation post number and story number. I am unaware of how to access all of this to append the struct.

这是具有用户注意事项的节点:

Here is the node with the user-considerations:

推荐答案

坐着,我真的没有看到代码有什么超级错误,但是在那里可以进行一些更改以使其更加简化。

As it sits, I am really not seeing anything super wrong with the code but there are few things that could be changed to make it more streamlined.

我将首先更改考虑信息类以使其更加独立。添加便利初始化程序,以通过一些错误检查直接处理firebase快照。

I would first change the Consideration Info class to make it more self contained. Add a convenience initializer to handle a firebase snapshot directly with some error checking.

class ConsiderationInfo: NSObject {
    var companyName = ""

    convenience init(withSnapshot: DataSnapshot) {
        self.init()
        self.companyName = withSnapshot.childSnapshot(forPath: "Company Name").value as? String ?? "No Company Name"
    }
}

我也建议删除.childAdded和.observe事件,除非您特别指定希望收到将来的更改通知。请改用.value和.observeSingleEvent。请记住,.childAdded一次遍历数据库中的每个节点-.value同时读取它们。如果数据量有限,.value效果很好。

I would also suggest removing the .childAdded and .observe events unless you specifically want to be notified of future changes. Use .value and .observeSingleEvent instead. Keeping in mind that .childAdded iterates over each node in your database one at a time - .value reads them in all at the same time. If there is a limited amount of data, .value works well.

    func updateConsiderationsArray() {
        let fbRef = Database.database().reference()
        let uid = Auth.auth().currentUser?.uid
        let ref = fbRef.child("user_considerations").child(uid)
        ref.observeSingleEvent(of: .value, with: { snapshot in
            let allUidsSnap = snapshot.children.allObjects as! [DataSnapshot]
            for uidSnap in allUidsSnap {
                let considerationId = uidSnap.key
                let considerationReference = fbRef.child("Considerations").child(considerationId)
                considerationReference.observeSingleEvent(of: .value, with: { snapshot in
                    let considerationInfo = ConsiderationInfo(withSnapshot: snapshot)
                    self.considerationArray.append(considerationInfo)
                    // update your collectionView
                })
            }
        })
    }

我在上面做的事情正在从user_considerations中读取单个节点,根据您的查询,它看起来像这样

What I am doing in the above is reading in the single node from user_considerations, which looks like this according to your quuestion

user_considerations
   some_user_uid
      a_user_uid
      a_user_uid

然后将每个子用户映射到一个数组以维持顺序

and then mapping each child user to an array to maintain order

let allUidsSnap = snapshot.children.allObjects as! [DataSnapshot]

然后遍历每个节点,获取每个节点的uid(键),并从中获取该节点的数据注意事项节点。

and then iterating over each, getting the uid (the key) of each node and getting that nodes data from the Considerations node.

这篇关于访问Firebase数据库中的多个节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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