gpt4 book ai didi

ios - 关注者计数器未更新 firebase 中的节点

转载 作者:可可西里 更新时间:2023-11-01 00:58:24 25 4
gpt4 key购买 nike

我一直在尝试在我的应用程序上实现“关注”功能。本质上,当用户点击“关注”按钮时,我们运行 runTransactionBlock 来更新我们存储在 Firebase 数据库中的用户及其关注帐户的整数值。 问题是我可以为用户更新计数器(比如下面例子中的 John),但是我不能为我关注的用户更新计数器(比如下面例子中的 olivia)。

目前 Firebase 节点看起来是这样的:

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

我已经引用了以下内容,但是我仍然面临着让它工作的问题。如果有人可以浏览并指出正确的方向,将不胜感激。

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)

})
}

最佳答案

尝试:-

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

关于ios - 关注者计数器未更新 firebase 中的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39601296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com