にそれが困難firebaseで値を更新するために見つける私は、フィールド名user_postを得ました。次に、childByAutoId()を使用して新しい投稿を追加しています。その中で私はの投稿IDに従って投稿を挿入しています。今度はをのように更新し、新しいフィールド peopleWhoLikeを追加します。誰でも私にどのようにを挿入してくださいまたは更新の任意の特定の投稿の値を教えてください。ここでシーケンス>)postId user_post>childByAutoId(あるは私のFirebaseデータベースはここでは、この</p> <p><a href="https://i.stack.imgur.com/pnh6l.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pnh6l.png" alt="enter image description here"></a></p> <p>のように見えるスウィフト
は私のコードです:ここでは
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