发现很难在 Swift 中更新 firebase 中的值 [英] Finding it difficult to update value in firebase in Swift

查看:28
本文介绍了发现很难在 Swift 中更新 firebase 中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Firebase 数据库如下所示

My Firebase database looks like this

这里有一个字段名称user_post.然后我使用 childByAutoId() 添加一个新帖子.然后在里面我根据 postId 插入帖子.现在我想更新 like 并添加新字段 peopleWhoLike.谁能告诉我如何插入更新任何特定帖子的值.顺序是user_post > childByAutoId() > postId

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

这是我的代码:

 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()

推荐答案

例如,以下是一次更新一个值的方法,

Here is how you can update one value at a time for example,

let ref : FIRDatabaseReference!
    ref = FIRDatabase.database().reference()
    ref.child("yourNode").child("subNode").updateChildValues(["yourKey": yourValue])

我所做的是写出我帖子的关键.创建帖子时,会使用 childByAutoID 并将其添加到帖子数据中,以便我以后可以引用它.我将它引用为键",值将是 childByAutoId 字符串.一旦我有了那个钥匙,我就可以添加一个赞,就像这样,

What I did was write the key to my posts. When a post was created a took the childByAutoID and added that to the post data so I could reference it later. I would reference it as "key" and the value would be the childByAutoId string. Once I had that key I would be able to add a like, like this,

点击

ref.child("yourNode").child(postKey).updateChildValues(["key": yourValue])

您可以在根节点创建一个名为喜欢"的新节点,无论何时用户喜欢或不喜欢它都会获取 postKey 并在 Likes 节点下创建一个子节点,然后添加 userId.当帖子加载时,您将进入 Likes 节点并匹配帖子,然后检查用户是否喜欢.

You could make a new node at the root called "Likes" whenever a user likes or unlikes it would take the postKey and create a subNode under the Likes node and then add the userId. When the post would load you would go into the Likes nodes and match the posts and then check it that user has liked or not.

这里我有用户关注列表,但它可能与喜欢的列表相同.在我的应用程序中,当用户保存帖子时.我在监视列表下记录 userId,在 userId 中是保存的 postId.这可能类似于喜欢.然后我只是比较一下,看看有没有匹配.

Here I have user-watchlists but it could be the same as likes. In my app when a user saves a post. I record the userId under watchlists and in the userId is the postId that was saved. This could be similar to likes. Then I just compare and see if there is a match.

这篇关于发现很难在 Swift 中更新 firebase 中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆