【发布时间】:2016-09-14 11:32:06
【问题描述】:
我正在尝试为我的 UITableViewCell 创建一个赞按钮。到目前为止,我设法做的是每次单击按钮时用 +1 更新喜欢的数量。但是,我只希望用户能够按一次按钮,如果同一用户再次单击该按钮,则用户不喜欢该帖子-就像 facebook 喜欢按钮一样。
如果有任何帮助,我将通过 facebook 登录我的应用程序。
我的代码:
@IBAction func likeTapped(sender: AnyObject) {
//print(pathDB)
FIRDatabase.database().reference().child("feed-items").child(pathDB).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
// Get user value
let antalLikes = snapshot.value!["likesForPost"] as! Int
print(antalLikes)
let dataPathen = self.pathDB
print(dataPathen)
self.updateTotalNoOfPost{
print("Updated")
}
// ...
}) { (error) in
print(error.localizedDescription)
}
}
func updateTotalNoOfPost(completionBlock : (() -> Void)){
let prntRef = FIRDatabase.database().reference().child("feed-items").child(pathDB).child("likesForPost")
prntRef.runTransactionBlock({ (resul) -> FIRTransactionResult in
if let dealResul_Initial = resul.value as? Int{
resul.value = dealResul_Initial + 1
//Or HowSoEver you want to update your dealResul.
return FIRTransactionResult.successWithValue(resul)
}else{
return FIRTransactionResult.successWithValue(resul)
}
}, andCompletionBlock: {(error,completion,snap) in
print(error?.localizedDescription)
print(completion)
print(snap)
if !completion {
print("Couldn't Update the node")
}else{
completionBlock()
}
})
}
希望你们能帮助我:)
【问题讨论】:
-
尝试在
prntRef.runTransactionBlock({ ...之前打印pathDB,你会得到什么?我的猜测是你会得到它 nil。你在哪里声明pathDB,你在哪里初始化它。? -
pathDB 正在初始化,我没有得到零,因为我正在打印 pathDB 的 dataPathen。代码没有问题,我只是寻求帮助来实现具有我上面提到的功能的类似按钮。你能做到吗?我想为你的时间付钱。
标签: ios facebook firebase firebase-realtime-database