Firebase在Swift中自动ID下检索数据 [英] Firebase retrieve data below auto ID In Swift
本文介绍了Firebase在Swift中自动ID下检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从Firebase检索数据时遇到麻烦.
I'm in trouble in retrieving data from Firebase.
我想在自动ID下读取JSON中的所有contactName数据,然后追加到UIPickerView.
I'd like to read all contactName data in JSON under auto ID , then append to UIPickerView.
这是我的JSON树(使用childByAutoId())
Here is my JSON tree (used childByAutoId())
这是我的Swift代码
And Here is my Swift Code
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)")
}
})
但是结果显示了我所有的零数据……看起来像这样.
But the result shows me all nil data... looks like this.
我该如何解决..?
推荐答案
我解决了自己!
但是首先,我决定不使用UIPickerView.
But first of all, I decided not to use UIPickerView.
我想做的是在自动ID下添加数据.
And what I wanna do is to add data below auto ID.
我不确定这是否是解决此问题的好算法,但是无论如何,我做到了:)
I'm not sure this is good algorithm for solving this problem, But Anyway, I made it :)
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)
}
}
})
}
}
})
这篇关于Firebase在Swift中自动ID下检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文