在Swift中,Firebase检索自动ID下方的数据

我在从Firebase检索数据时遇到了麻烦。

我想在自动ID下读取JSON中的所有contactName数据,然后附加到UIPickerView。

这是我的JSON树(使用childByAutoId()) 在这里输入图像说明

这是我的Swift代码

dbRef = Database.database().reference() dbRef.child("user").child("contacts").queryOrdered(byChild: "contactName").observeSingleEvent(of: .value, with: {(snapshot) in for snap in snapshot.children { let userSnap = snap as! DataSnapshot let contactName = userSnap.value as? String self.pickOption.append("\(contactName)") } }) 

但结果显示我所有的零数据…看起来像这样。

在这里输入图像说明

我如何解决它..?

我解决了我自己!

但首先,我决定不使用UIPickerView。

而我想要做的是在自动ID下面添加数据。

我不知道这是解决这个问题的好algorithm,但无论如何,我做到了:)

 dbRef.child("user/contacts/").observe(.value, with: {(snapshot) in if let result = snapshot.children.allObjects as? [DataSnapshot] { for child in result { let orderID = child.key as String //get autoID self.dbRef.child("user/contacts/\(orderID)/contactName").observe(.value, with: { (snapshot) in if let nameDB = snapshot.value as? String { if self.debtorName == nameDB { self.dbRef.child("user/contacts/\(orderID)").updateChildValues(data) } } }) } } })