Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.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
Ios 访问Firebase数据库Swift 3中的嵌套子项_Ios_Swift_Firebase_Swift3_Firebase Realtime Database - Fatal编程技术网

Ios 访问Firebase数据库Swift 3中的嵌套子项

Ios 访问Firebase数据库Swift 3中的嵌套子项,ios,swift,firebase,swift3,firebase-realtime-database,Ios,Swift,Firebase,Swift3,Firebase Realtime Database,我当前的firebase数据库结构如下 customer -L1x2AKUL_KNTKXyza name:"abc" subscription -L1x2AKlvmG0RXv4gL sub_no: "123" sub_name: "" -L1x2AKlvmG0RXv4ab sub_no: "456" sub_name" "" -L1x2AKUL_KNTKXymk name:"x

我当前的firebase数据库结构如下

customer
  -L1x2AKUL_KNTKXyza
    name:"abc"
    subscription
      -L1x2AKlvmG0RXv4gL
        sub_no: "123"
        sub_name: ""
      -L1x2AKlvmG0RXv4ab
        sub_no: "456"
        sub_name" ""
  -L1x2AKUL_KNTKXymk
    name:"xyz"
    subscription
      -L1x2AKlvmG0RXv4xy
        sub_no: "789"
        sub_name: ""
我试图一次访问所有客户记录的所有子说明

这是我正在使用的代码:

var ref: DatabaseReference!

ref = Database.database().reference(withPath: "customer")

ref.observe(.value, with: { snapshot in
        let enumerator = snapshot.children

        while let rest = enumerator.nextObject() as? DataSnapshot {

            let imageSnap = rest.childSnapshot(forPath: "subscription")
            let dict = imageSnap.value as! NSDictionary

            //self.vehicleListDict.append(dict.object(forKey: "sub_no") as! NSDictionary)

            print("value : \(dict)")

        }
        print("vehicleListDict : \(self.vehicleListDict)")
    }) { (error) in
        print(error.localizedDescription)
    }
我无法同时访问所有客户记录中的所有订阅。它只有一个级别。我试图在存在的while中设置一个while循环,但这也不能提供所需的输出。它以无限循环的方式运行。请任何人帮忙。我第一次使用firebase实时数据库

获取的值应该是

123
456
789

具体执行您要求的操作的代码是

let customerRef = self.ref.child("customer")
customerRef.observe(.childAdded, with: { snapshot in
    let subscriptionSnap = snapshot.childSnapshot(forPath: "subscription")
    for child in subscriptionSnap.children {
        let snap = child as! DataSnapshot
        let dict = snap.value as! [String: Any]
        let subNo = dict["sub_no"] as! String
        print(subNo)
    }
})
输出是

a123
a456
a789
a123
a456
a789
*请注意,我将sub_no作为字符串读取,这就是我在前面添加“a”的原因。如果它们实际上是整数,则将行更改为

let subNo = dict["sub_no"] as! Integer
*注2:这将在所讨论的主节点上留下一个.childAdded的观察者,因此添加的任何其他子节点都将触发闭包中的代码

编辑:

如果您只想一次检索所有数据,而不想留下一个childAdded observer,则可以这样做:

let customerRef = self.ref.child("customer")
customerRef.observeSingleEvent(of: .value, with: { snapshot in
    for customerChild in snapshot.children {
        let childSnap = customerChild as! DataSnapshot
        let subscriptionSnap = childSnap.childSnapshot(forPath: "subscription")
        for subscriptionChild in subscriptionSnap.children {
            let snap = subscriptionChild as! DataSnapshot
            let dict = snap.value as! [String: Any]
            let subNo = dict["sub_no"] as! String
            print(subNo)
        }   
    }
})
输出是

a123
a456
a789
a123
a456
a789

你的要求毫无意义。我需要一份所有订阅的列表。。它们与每个客户的详细信息一起存储。。所以需要以这种方式遍历它,这些函数应该在谷歌的云函数中完成。这很容易做到。但是,您能否解释一下为什么需要这些数据(即,用例是什么?为什么要同时为所有客户提供所有订阅),因为这可能需要Firebase结构的重新工具才能真正获得所需的数据。@Jay有一个下拉菜单,其中需要显示所有订阅。我无法更改firebase结构。必须从当前结构本身获取。你能帮忙吗。。我还在为此苦苦挣扎谢谢你的回复,杰!在获取嵌套子对象方面,我还有一个类似的问题。你能为同一个问题发起一次聊天吗?@Dia如果这是一个不同的问题,那么将其作为一个问题发布,我们将查看。