我正在努力更新FirebaseDatabase
中的多个runTransactionBlock
节点。此块应增加likesCount
和recordsCount
。最初,当发布一个帖子时,那些被设置为0.所以,我检查likesCount == 0
是否是,这意味着这是第一个喜欢帖子所以它只需在单独的节点中使用userUid设置likes
数组,并设置值true
。然后递增此节点中的likesCount
和recordsCount
以及runTransactionBlock
以更新其值。第一次,这是有效的。它设定了它们的价值。
func incrementLikes(postId: String, onSuccess: @escaping (Post) -> Void, onError: @escaping (_ errorMessage: String?) -> Void) {
let postRef = Api.Post.REF_POSTS.child(postId)
postRef.runTransactionBlock({ (currentData) -> TransactionResult in
if var post = currentData.value as? [String : AnyObject] {
var likesCount = post["likesCount"] as? Int ?? 0
var recordsCount = post["recordsCount"] as? Int ?? 0
if likesCount == 0 { // First like for the post
// add self in posts_likes > postId > likes
// increment likes count & increment records Count
Api.Posts_Likes.firstPostsLikes(postId: postId)
likesCount += 1
recordsCount += 1
}
else {
Api.Posts_Likes.setPostsLikes(postId: postId, onSuccess: { (isPostLiked) in // checks if user liked the post or not, if he did, his id was added in posts_likes > likes
if isPostLiked {
likesCount += 1
recordsCount += 1
}
else {
likesCount -= 1
recordsCount -= 1
}
}, onError: { (errMessage) in
onError(errMessage)
})
}
post["recordsCount"] = recordsCount as AnyObject?
post["likesCount"] = likesCount as AnyObject?
currentData.value = post
return TransactionResult.success(withValue: currentData)
}
return TransactionResult.success(withValue: currentData)
})
{ (error, committed, snapshot) in
if let error = error {
onError(error.localizedDescription)
}
if let dict = snapshot?.value as? [String: Any] {
let post = Post.transformDataToImagePost(dictionary: dict, key: snapshot!.key)
onSuccess(post)
}
}
}
当用户再次点击该按钮时,它会进入else
语句,因为likesCount
不再等于0
。这是它无法正常工作的地方。调用Api.Posts_Likes.setPostsLikes(postId: postId, onSuccess:)
时,无论用户是否喜欢该帖子,都应返回Bool
值。根据{{1}}值,它应bool
或increment
decrement
& likesCount
节点中的recordsCount
。但它并没有,我不明白为什么。
posts
这就是 func firstPostsLikes(postId: String) {
guard let currentUser = Api.Users.CURRENT_USER else { return }
REF_POSTS_LIKES.child(postId).child("likes").child(currentUser.uid).setValue(true)
}
func setPostsLikes(postId: String, onSuccess: @escaping (Bool) -> Void, onError: @escaping (_ errorMessage: String?) -> Void) {
let postLikesRef = Api.Posts_Likes.REF_POSTS_LIKES.child(postId)
guard let currentUser = Api.Users.CURRENT_USER else { return }
var userLikedPost: Bool = false
postLikesRef.runTransactionBlock ({ (currentPostsLikesData) -> TransactionResult in
if var postLikes = currentPostsLikesData.value as? [String : AnyObject] {
var likes = postLikes["likes"] as? [String : Bool] ?? [:]
if let _ = likes[currentUser.uid] {
// Unlike the post and remove self from likes
likes.removeValue(forKey: currentUser.uid)
userLikedPost = false
}
else {
// Like the post, add self to posts_likes, increase likesCount & recordsCount
likes[currentUser.uid] = true
userLikedPost = true
}
postLikes["likes"] = likes as AnyObject
currentPostsLikesData.value = postLikes
return TransactionResult.success(withValue: currentPostsLikesData)
}
return TransactionResult.success(withValue: currentPostsLikesData)
})
{ (error, committed, snapshot) in
if let error = error {
onError(error.localizedDescription)
}
onSuccess(userLikedPost)
}
的样子:
FirebaseDatabase
节点中的recordsCount
是posts
,commentsCount
,likesCount
和repostsCount
的总和。在单元格中加载新applaudsCount
时,我唯一需要显示的是post
。这就是为什么我不想在帖子本身下放置所有喜欢,赞扬,评论等等的原因,因为除非用户点击recordsCount
,否则它将无需下载大量数据按钮实际