My Firebase database looks like this
Here I got a field name user_post. Then I am adding a new post using childByAutoId() . Then inside that I am inserting the post according the postId . Now I want to update the like and add the new field peopleWhoLike. Could anybody tell me how to insert or update a value of any particular post. The sequence is user_post > childByAutoId() > postId
Here is my code:
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() 
