Firebase TransactionResult未提交?

时间:2018-08-15 10:20:57

标签: swift firebase firebase-realtime-database

我正在尝试在另一个runTransactionBlock中运行一个runTransactionBlock。问题是我看到了数据机会(async方式),但是从未将其提交到数据库。我不明白为什么。

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 (if first like for the app, it sets "posts_likes" node on FB)
                Api.Posts_Likes.firstPostsLikes(postId: postId)
                likesCount += 1
                recordsCount += 1
            }
            else {
                Api.Posts_Likes.setPostsLikes(postId: postId, onSuccess: { isPostLikedBool  in  // checks if user liked the post, if he did, his id was added in posts_likes > id > likes
                    print("1")
                    if isPostLikedBool {
                        likesCount += 1
                        recordsCount += 1
                    }
                    else {
                        likesCount -= 1
                        recordsCount -= 1
                    }
                    print("Like count in set posts: \(likesCount)")
                    post["recordsCount"] = recordsCount as AnyObject?
                    post["likesCount"] = likesCount as AnyObject?
                    print("2")
                    currentData.value = post

                    print("Bitch value \(currentData)")

                    return TransactionResult.success(withValue: currentData)
                })
                post["recordsCount"] = recordsCount as AnyObject?
                post["likesCount"] = likesCount as AnyObject?
                currentData.value = post
                print("3")
                return TransactionResult.success(withValue: currentData) // passed this a return value in the Api.Posts_Likes.setPostsLikes() 
            }
            print("4")

            post["recordsCount"] = recordsCount as AnyObject?
            post["likesCount"] = likesCount as AnyObject?
            currentData.value = post
            return TransactionResult.success(withValue: currentData)
        }
        print("5")
        return TransactionResult.success(withValue: currentData)
    })
    { (error, committed, snapshot) in
        if let error = error {
            onError(error.localizedDescription)
        }
        if committed {
            print(committed)
        }

        if let dict = snapshot?.value as? [String: Any] {
            let post = Post.transformDataToImagePost(dictionary: dict, key: snapshot!.key)
            onSuccess(post)
            print(post.likesCount)
        }
    }
}

大图

我们有2个节点:postsposts_likes(后一个是映射节点)。 在数据库中存储新帖子时,posts节点包含likesCount: 0

最先点按“赞”按钮,将创建具有该帖子唯一ID的节点posts_likes,并在其中创建一个likes对象,该对象存储uid: true(喜欢该帖子的用户)。

同一用户第二次点击“赞”按钮,它将检查Bool的{​​{1}}值以增加或减少isPostLikedBool

我知道这都是异步发生的,我使用print语句对其进行了测试,但是在执行的最后一点,likeCountlikeCount,但是从未提交过此块。

这是我的问题,我不确定为什么没有像以前一样提交它。

0 个答案:

没有答案