我在从 Firebase 检索数据时遇到麻烦。
我想读取 auto ID 下 JSON 中的所有 contactName 数据,然后附加到 UIPickerView。
Here is my JSON tree (used 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 下面添加数据。
我不确定这是解决这个问题的好算法,但无论如何,我做到了:)
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)
}
}
})
}
}
})
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)