更新火力值我的火力地堡數據庫看起來像這樣發現很難在斯威夫特
這裏我有一個字段名user_post。然後我使用childByAutoId()添加一個新帖子。然後在裏面,我插入帖子根據postId。現在我想更新,如,並添加新字段peopleWhoLike。有誰可以告訴我如何插入或更新任何特定職位的值。順序是 user_post>childByAutoId()>帖子ID
這裏是我的代碼:
let keyToPost = ref.child("user_post").childByAutoId().key
ref.child("user_post").queryOrdered(byChild: self.postID).observeSingleEvent(of: .value, with: { (snapshot) in
if (snapshot.value as? [String : AnyObject]) != nil {
let updateLikes: [String : Any] = ["peopleWhoLike/\(keyToPost)" : FIRAuth.auth()!.currentUser!.uid]
ref.child("user_post").child(self.postID).updateChildValues(updateLikes, withCompletionBlock: { (error, reff) in
if error == nil {
ref.child("user_post").child(self.postID).observeSingleEvent(of: .value, with: { (snap) in
if let properties = snap.value as? [String : AnyObject] {
if let likes = properties["peopleWhoLike"] as? [String : AnyObject] {
let count = likes.count
let update = ["Like" : count]
ref.child("user_post").child(self.postID).updateChildValues(update)
self.likeBtn.isHidden = true
self.unlikeBtn.isHidden = false
self.likeBtn.isEnabled = true
}
}
})
}
})
}//xxx
})
ref.removeAllObservers()
添加了更詳細的答案。 – Ro4ch