追随者计数器不更新Firebase中的节点 [英] Follower counter not updating node in firebase

查看:151
本文介绍了追随者计数器不更新Firebase中的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图在我的应用程序上实现跟随功能。实际上,当用户点击关注按钮时,我们运行一个 runTransactionBlock 来更新我们存储在Firebase数据库中的用户和他们关注的帐户的整数值。 问题是我能够更新用户的计数器(比如下面的例子中的John),但是我无法更新我正在关注的用户的计数器(比如下面的奥利维亚例子)。

目前Firebase节点看起来像这样:

$ p $ user_profiles {
UID1:{
名称:john
以下内容:1 //代码将更新我的帐户
追随者:0
},
UID2:{
名称:olivia
以下:0
追随者:0 //代码不会更新人数,我正在尝试追踪

我引用了下面的内容,但是我仍然面临着这个问题。如果任何人都可以请扫一眼,指向正确的方向,这将不胜感激。



https ://www.firebase.com/docs/ios/guide/saving-data.html



  var guestUIDToPass = String()
var loggedInUser = AnyObject()
$ b @IBAction func didTapFollow(sender:AnyObject){
following()




func(){

self.loggedInUser = FIRAuth.auth()?。currentUser


//为用户更新计数,完美工作
$ b $ self.databaseRef.child(user_profiles)。child(self.loggedInUser.uid).child(following)。 runTransactionBlock({
(currentData:FIRMutab leData!)in
var value = currentData.value as? Int
if(value == nil){
value = 0
}
currentData.value = value! + 1
返回FIRTransactionResult.successWithValue(currentData)
})

//更新次数为用户所关注,不更新firebase

($ {$ b $(currentData:FIRMutableData!)in
var(user_profiles value = currentData.value as?Int
if(value == nil){
value = 0
}
currentData.value = value!+ 1
返回FIRTransactionResult .successWithValue(currentData)

})
}


解决方案

试试:


$ b $

  let prntRef = FIRDatabase.database()。 (user_profiles)。child(whoifollowedUID).child(following)

prntRef.runTransactionBlock({(following) - >
中的FIRTransactionResult如果let followNum = followin g.value as?Int {

following.value = followNum + 1
return FIRTransactionResult.successWithValue(following)
} else {

return FIRTransactionResult .successWithValue(以下)

$ b},和CompletionBlock:{(错误,完成,快照)

print(error?.localizedDescription)
打印(完成)
打印(快照)
if!完成{

print(值无法更新)
} else {
//已更新
}
})


I have been trying to implement a "follow" function on my app. Essentially when a user hits the "follow" button, we run a runTransactionBlock to update integer values we are storing on Firebase database for the user and the account they are following. The issue is that I am able to update the counter for the user (say John in example below) , but I am not able to update the counter for the user I am following (say olivia in example below).

Currently the Firebase nodes look as such:

user_profiles{
      UID1:{
           name: john
           following: 1 //code will update for my account
           followers: 0
      },
      UID2:{
           name: olivia
           following: 0
           followers: 0 //code will not update count for person i am trying to follow

I have referenced the following, however I am still facing issues with getting this to work. If anyone can please glance through and point me in the right direction, it would be greatly appreciated.

https://www.firebase.com/docs/ios/guide/saving-data.html

Firebase database help - Swift

Upvote/Downvote system within Swift via Firebase

var guestUIDToPass = String()
var loggedInUser = AnyObject()

@IBAction func didTapFollow(sender: AnyObject) {
 following() 
}



func following() {

        self.loggedInUser = FIRAuth.auth()?.currentUser


//updating count for user, works perfectly        

self.databaseRef.child("user_profiles").child(self.loggedInUser.uid).child("following").runTransactionBlock({
            (currentData:FIRMutableData!) in
            var value = currentData.value as? Int
            if (value == nil) {
                value = 0
            }
            currentData.value = value! + 1
            return FIRTransactionResult.successWithValue(currentData)
        })

//updating count for person user is following, doesn't update firebase

        self.databaseRef.child("user_profiles").child("\(self.guestUIDToPass)").child("followers").runTransactionBlock({
            (currentData:FIRMutableData!) in
            var value = currentData.value as? Int
            if (value == nil) {
                value = 0
            }
            currentData.value = value! + 1
            return FIRTransactionResult.successWithValue(currentData)

        })
    }

解决方案

Try:-

let prntRef = FIRDatabase.database().reference().child("user_profiles").child(whomIFollowedUID).child("following")

prntRef.runTransactionBlock({ (following) -> FIRTransactionResult in
    if let followNum = following.value as? Int{

        following.value = followNum + 1
        return FIRTransactionResult.successWithValue(following)
    }else{

        return FIRTransactionResult.successWithValue(following)

    }
    }, andCompletionBlock: {(error,completion,snap) in

            print(error?.localizedDescription)
            print(completion)
            print(snap)
        if !completion {

            print("The value wasn't able to Update")
            }else{
            //Updated
        }
})

这篇关于追随者计数器不更新Firebase中的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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