从Firebase删除帖子

时间:2018-11-27 17:23:04

标签: swift firebase

如果该帖子具有相等/多于5的不喜欢,则我想删除该帖子。我实现了计数器部分,并且该方法已经起作用。但是,即使我有以下6条类似的帖子,也不会删除该帖子:

在这里您可以看到我的数据库。

enter image description here

这是代码,我要在5次不喜欢后删除帖子:

// Dislike Button

func addTapGestureToDislikeImageView() {
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleDidTapDislike))
    dislikeImageView.addGestureRecognizer(tapGesture)
    dislikeImageView.isUserInteractionEnabled = true

}

@objc func handleDidTapDislike() {
    guard let postId = post?.id else { return }

    PostApi.shared.incrementDislikes(postId: postId, onSuccess: { (post) in
        self.updateDislike(post: post)
        self.post?.dislikes = post.dislikes
        self.post?.isDisliked = post.isDisliked
        self.post?.dislikeCount = post.dislikeCount

    }, onError: { (errorMessage) in
        ProgressHUD.showError(errorMessage)
    })
}

func updateDislike(post: PostModel) {
    if post.isDisliked == false || post.dislikes == nil {
        dislikeImageView.image = UIImage(named: "icons8-gefaellt-nicht-50")
    } else {
        dislikeImageView.image = UIImage(named: "icons8-gefaellt-nicht-filled-50")
    }

    guard let count = post.dislikeCount else { return }
    if count >= 5 {
        deletePost()
    }
}

func deletePost() {
    // Remove the post from the DB
    let ref = Database.database().reference()
    ref.child("posts").child((post?.id)!).removeValue { error,ref   in
        if error != nil {
            print(error!.localizedDescription)
        }
    }
}

在这里我增加不喜欢的次数:

func incrementDislikes(postId id: String, onSuccess: @escaping (PostModel) -> Void, onError: @escaping (_ errorMessage: String?) -> Void) {

    let postRef = REF_POSTS.child(id)

    postRef.runTransactionBlock({ (currentData: MutableData) -> TransactionResult in
        if var post = currentData.value as? [String : AnyObject], let uid = UserApi.shared.CURRENT_USER_ID {

            var dislikes: Dictionary<String, Bool>
            dislikes = post["dislikes"] as? [String : Bool] ?? [:]
            var dislikeCount = post["dislikeCount"] as? Int ?? 0
            if let _ = dislikes[uid] {
                // Unstar the post and remove self from stars
                dislikeCount -= 1
                dislikes.removeValue(forKey: uid)
            } else {
                // Star the post and add self to stars
                dislikeCount += 1
                dislikes[uid] = true
            }
            post["dislikeCount"] = dislikeCount as AnyObject?
            post["dislikes"] = dislikes as AnyObject?

            // Set value and report transaction success
            currentData.value = post

            return TransactionResult.success(withValue: currentData)
        }
        return TransactionResult.success(withValue: currentData)
    }) { (error, committed, snapshot) in
        if let error = error {
            onError(error.localizedDescription)
        }
        guard let dic = snapshot?.value as? [String: Any] else { return }
        guard let postId = snapshot?.key else { return }
        let updatePost = PostModel(dictionary: dic, key: postId)
        onSuccess(updatePost)
    }
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我认为您需要共享更多信息或看到的任何错误,或者对其进行调试并测试操作在何处停止。但是尝试这个也许会起作用。

@objc func handleDidTapDislike() {
guard let postId = post?.id else { return }

PostApi.shared.incrementDislikes(postId: postId, onSuccess: { (post) in
    self.updateDislike(post: post)
    self.post?.dislikes = post.dislikes
    self.post?.isDisliked = post.isDisliked
    self.post?.dislikeCount = post.dislikeCount
    if post.dislikeCount > 4 { 
         deletePost()
    }
}, onError: { (errorMessage) in
    ProgressHUD.showError(errorMessage)
})}

然后编辑updateDislike()函数

func updateDislike(post: PostModel) {
   if post.isDisliked == false || post.dislikes == nil {
       dislikeImageView.image = UIImage(named: "icons8-gefaellt-nicht-50")
   } else {
       dislikeImageView.image = UIImage(named: "icons8-gefaellt-nicht-filled-50")
   }
}