我正在尝试获取在警报子节点中编写的autoId并将其转换为字符串,以便从问题节点中获取此autoId以在其中获取信息-我的代码可以运行但无法打印/ work
(当前用户autoId是警报节点的第一个子级,而userId是来自“ objectId” /问题的第二个子级的字符串)。这是我的代码:
@IBOutlet weak var statementAnswered: UILabel
@IBOutlet weak var number1Label: UIButton!
@IBOutlet weak var number2Label: UIButton!
@IBOutlet weak var number3Label: UIButton!
@IBOutlet weak var number4Label: UIButton!
guard let currentUser = Auth.auth().currentUser?.uid else { return }
let ref = Database.database().reference()
//enter alert node to get the new Id under the current user node
let userRef = ref.child("alert").child(currentUser)
userRef.observe(.childAdded, with: { (snapshot) in
let uniqueKey = snapshot.key
print(uniqueKey)
guard let senderDetails = snapshot.childSnapshot(forPath: "from").value as? String else { return }
guard let userID = snapshot.childSnapshot(forPath: "objectID").value as? String else { return }
print(senderDetails)
print(userID)
let questionNotification = ref.child("questions").child(senderDetails).child(userID)
questionNotification.observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
let statement = value?["statement"] as! String
let number1 = value?["number 1"] as! String
let number2 = value?["number 2"] as! String
let number3 = value?["number 3"] as! String
let number4 = value?["number 4"] as! String
print(statement)
print(number1)
print(number2)
print(number3)
print(number4)
self.statementAnswered.text = question
self.number1Label.setTitle(number1, for: .normal)
self.number2Label.setTitle(number2, for: .normal)
self.number3Label.setTitle(number3, for: .normal)
self.number4Label.setTitle(number4, for: .normal)
})
}, withCancel: nil)
这是JSON结构的代码段:
"alert" : {
"FjS4wUpXAUa5aWwXkjvujHxE4He2" : {
"-La2uIRfXs15WP8bEfQo" : {
"from" : "Gmg1ojNoBiedFPRNSL4sBZz2gSx2",
"objectId" : "-La2uIRPzNXnamX4QLER",
"timestamp" : 1552696293
}
}
},
"questions" : {
"Gmg1ojNoBiedFPRNSL4sBZz2gSx2" : {
"-La2uIRPzNXnamX4QLER" : {
"number 1" : "4444",
"number 2" : "1111",
"number 3" : "2222",
"number 4" : "3333",
"statement" : "favourite number",
"selected" : "FjS4wUpXAUa5aWwXkjvujHxE4He2"
}
}
},
感谢任何帮助!
更新:
我已经更新了代码,并在控制台上打印了以下内容(应用程序上的用户的autoId):
[Snap (CjeP35ceAQZJuUPhm7U1eF3Yq4F3) 1, Snap
(FjS4wUpXAUa5aWwXkjvujHxE4He2) 1, Snap (FoFQDAGGX9hntBiBdXYCBHd8yas2)
1, Snap (PqMkClaPM3W8k7ZSgzAHb3yne5D3) 1]
[Snap (CjeP35ceAQZJuUPhm7U1eF3Yq4F3) 1, Snap
(FjS4wUpXAUa5aWwXkjvujHxE4He2) 1, Snap (FoFQDAGGX9hntBiBdXYCBHd8yas2)
1, Snap (PqMkClaPM3W8k7ZSgzAHb3yne5D3) 1]
[Snap (CjeP35ceAQZJuUPhm7U1eF3Yq4F3) 1, Snap
(FjS4wUpXAUa5aWwXkjvujHxE4He2) 1, Snap (FoFQDAGGX9hntBiBdXYCBHd8yas2)
1, Snap (PqMkClaPM3W8k7ZSgzAHb3yne5D3) 1]
(我将继续尝试使我的断点正常工作,以便找出问题所在,并在可行的情况下在此处进行更新)
预先感谢
更新:
我已经更改了数据库的结构,以使“问题”不会保存在currentUser的autoId(回答问题的人员)下,而只是创建了一个随机ID,尽管我没有认为这不是最有效的方式
答案 0 :(得分:2)
问题1:
userRef.child(uniqueKey).observeSingleEvent(of:.value,with:{snapshot})
由于您已经通过观察Alert节点获得了所需的信息,因此无需再次观察用户参考。
问题2:
让questionNotification = self.ref.child(“问题”).child(senderDetails).child( self.userId )
在这一行中,您将userID引用到self
。查看您的JSON,很明显该字段将是动态的,因此在全局范围内进行遍历是不明智的。
警告:
您需要进行命名约定。您正在分配的变量名甚至与您收到的JSON响应都不接近。当您的项目规模变大时,如果您遇到任何错误,您将很难找到问题,因为您无法确定是哪个领域引起的。另外,请查看Codable
协议。编码和解码JSON对象将使您的生活更加轻松。
解决方案:
guard let currentUser = Auth.auth().currentUser?.uid else { return }
let ref = Database.database().reference()
//enter alert node to get the new Id under the current user node
let userRef = ref.child("alert").child(currentUser)
userRef.observe(.childAdded, with: { (snapshot) in
let uniqueKey = snapshot.key
print(uniqueKey)
guard let senderDetails = snapshot.childSnapshot(forPath: "from").value as? String else { return }
guard let userID = snapshot.childSnapshot(forPath: "objectID").value as? String else { return }
print(senderDetails)
print(userID)
let questionNotification = ref.child("questions").child(senderDetails).child(userID)
questionNotification.observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
let statement = value?["statement"] as! String
let number1 = value?["number 1"] as! String
let number2 = value?["number 2"] as! String
print(statement)
print(number1)
print(number2)
})
}, withCancel: nil)