You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

社交网络学习项目:Firebase评论删除的Comment ID获取问题

Fixing Comment Deletion with Firebase in Your iOS Social Network App

Hey there! Let's get that comment deletion working for you. The good news is you already have all the pieces you need—your Comment model already stores the unique Firebase ID for each comment, you just need to leverage it in your deletion logic. Here's how to do it step by step:

1. Add a Delete Method to Your Comment API

First, let's add a reusable method to your CommentApi class that handles deleting both the comment itself and its reference in the post-comments relationship (since you're using a denormalized structure for performance):

class CommentApi {
    var REF_COMMENTS = Database.database().reference().child("comments")
    
    // ... existing methods ...
    
    func deleteComment(postId: String, commentId: String, completion: @escaping (Error?) -> Void) {
        // 1. Delete the comment from the main comments node
        REF_COMMENTS.child(commentId).removeValue { error, _ in
            if let error = error {
                completion(error)
                return
            }
            // 2. Delete the comment reference from the post's comments list
            Api.Post_Comment.REF_POST_COMMENTS.child(postId).child(commentId).removeValue(completionBlock: completion)
        }
    }
}

2. Update Your Table View Deletion Logic

Now, in your CommentViewController, update the tableView(_:commit:forRowAt:) method to fetch the comment ID, validate permissions (optional but recommended), and trigger the deletion:

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        // Get the comment to delete from your local array
        let targetComment = comments[indexPath.row]
        
        // Make sure we have a valid comment ID
        guard let commentId = targetComment.id else {
            ProgressHUD.showError("Could not find comment ID")
            return
        }
        
        // Optional: Only let the comment author delete their own comment
        guard let currentUserId = Api.User.CURRENT_USER?.uid else { return }
        if targetComment.uid != currentUserId {
            ProgressHUD.showError("You can only delete your own comments")
            return
        }
        
        // Call the delete API
        Api.Comment.deleteComment(postId: self.postId, commentId: commentId) { error in
            if let error = error {
                ProgressHUD.showError(error.localizedDescription)
                return
            }
            
            // Update local data and refresh the table view
            self.comments.remove(at: indexPath.row)
            self.users.remove(at: indexPath.row) // Match the comments array since they're paired
            tableView.deleteRows(at: [indexPath], with: .fade)
        }
    }
}

3. Verify Your Comment Model is Correct

Just a quick check: your Comment model already captures the Firebase ID via the transformComment method, which assigns snapshot.key to comment.id—that's exactly what we need here, so no changes are needed here.

Why This Works

  • Your comments array holds fully populated Comment objects, each with their unique Firebase ID stored in the id property.
  • The deletion method cleans up both the comment data and the cross-reference in the post's comment list, keeping your Firebase database consistent.
  • The optional permission check ensures users can't delete others' comments, which is a key feature for any social app.

内容的提问来源于stack exchange,提问作者mental

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 11:32:47