Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从另一个孩子那里得到孩子Firebase Swift_Swift_Database_Xcode_Firebase - Fatal编程技术网

如何从另一个孩子那里得到孩子Firebase Swift

如何从另一个孩子那里得到孩子Firebase Swift,swift,database,xcode,firebase,Swift,Database,Xcode,Firebase,我想获得另一个孩子的孩子数据 以下是我尝试过的: private let ref = Database.database().reference().child("categories") viewDidLoad(){ self.ref.observeSingleEvent(of: .value, with: { (snapshot) in for user_child in (snapshot.children) {

我想获得另一个孩子的孩子数据 以下是我尝试过的:

 private let ref = Database.database().reference().child("categories")
   viewDidLoad(){
self.ref.observeSingleEvent(of: .value, with: { (snapshot) in
            for user_child in (snapshot.children) {
                
                let user_snap = user_child as! DataSnapshot
                let dict = user_snap.value as! [String: Any]
                
                self.categoryId.append(user_snap.key)
                
            }
        })

尝试挖掘Firebase子数据有时会在一开始就令人困惑

我们的理念是尽可能长时间地将数据保留为Firebase数据。这意味着不必强制转换到字典,而是让数据作为DataSnapshots存在。嗯,数据快照很酷;它们是很好的容器,非常灵活,非常容易使用

另外,将DataSnapshot的子快照强制转换到数组中的顺序保持不变,这对于迭代这些子快照来说是非常好的

这里有一个可能的解决方案来读取您问题中的数据。没有错误检查,我们正在强制展开一些选项,以便在您自己的代码中进行更改

一路上我添加了一些评论

func readCatAndChildren() {
    let catRef = self.ref.child("categories") //self.ref points to our firebase
    catRef.observeSingleEvent(of: .value, with: { snapshot in

        //first get all of the top level categories which have uid's as their keys
        let allUidSnaps = snapshot.children.allObjects as! [DataSnapshot] //put into an array, preserves order
        
        //now iterate over each child
        for uidSnap in allUidSnaps {
            let uid = uidSnap.key //get the uid for each child
            print("uid: \(uid)") //print it to console

            //each top level node has an items child which is a snapshot
            //  get the children of items and put them
            //  into an array - preserves order
            let itemsSnap = uidSnap.childSnapshot(forPath: "items") 
            let allItemsSnap = itemsSnap.children.allObjects as! [DataSnapshot] 

            //iterate over each item child node, which is category id
            for itemSnap in allItemsSnap { 
                let itemCatKey = itemSnap.key //get the child category id
                
                // finally, get the child category name from the child category
                let itemCatName = itemSnap.childSnapshot(forPath: "cat_name").value as? String ?? "No Item Cat Name"
                print("  itemCatKey: \(itemCatKey)    catName: \(itemCatName)") //print it out
            }
        }
    })
}

那么你得到了什么或者有错误吗?没有得到任何数据或者错误@zeytinHow,什么时候检查数据?