我有一个firebase应用程序,我需要调整键值对" yesCount",一个firebase autoID下的子项。每个帖子都有一个yes计数,我需要能够在按下每个单元格的按钮时调整正确孩子的yesCount(协调每个帖子)。 这是我的firebase数据库树
后物品 -L31FSpR8vy2TsBZy3-1
addedByUser: "user"
noText: "NO"
question: "Hello World?"
yesCount: 0
yesText: "YES"
后物品─> childByAutoID() - > yesCount 当我按下相应表格视图单元格上的按钮时,我需要能够在后期项目下更改每个帖子的yesCount。 我目前的想法是......
@IBAction func askTapped(_ sender: Any) {
//post under childByAutoID
let post = Post(question: daquestion, addedByUser: UserDefaults.standard.object(forKey: "name") as! String, yesText: dayes, noText: dano, yesCount: 0)
let postRef = dbRef.childByAutoId()
let postKey = postRef.key
var autoIDs = [""]
autoIDs.insert(postKey, at: 0)
print (" child id\(postKey)")
postRef.setValue(post.toAnyObject())
previousVC.tableView.reloadData()
navigationController?.popViewController(animated: true)
}
添加信息,然后检索它......
@IBAction func yesTapped(_ sender: UIButton) {
self.Yescount = Yescount + 1
dbRef.child("autoID").child("yesCount").setValue(Yescount)
let childCount = dbRef.child("autoID").value(forKey: "yesCount") as! Int
sender.setTitle("\(String(describing: childCount))", for: [])
dbRef.observe(.childChanged, with: { snapshot in
})
}
但是这不起作用(请记住,这些代码段都在单独的视图控制器中)。我对任何解决方案的想法持开放态度,并且更喜欢任何不需要重构我的代码和firebase数据库的想法。