gpt4 book ai didi

ios - 如何检索 autoId 以删除值

转载 作者:行者123 更新时间:2023-11-28 13:28:47 25 4
gpt4 key购买 nike

我试图让用户在按下删除按钮时删除评论。当评论被提交时,它们是使用 autoId 创建的,节点的标题将是 postId 以查看他们评论了什么帖子。

  "comments" : {
"-LmfZZis5ovtBwfm_4xR" : {
"-LoHu5Qv3BmuHTsSlthj" : {
"creationDate" : 1.567980283717026E9,
"text" : "Kkkk",
"uid" : "64r3dgTN6xMhHYhptFlsFWX0dLk2"
},
"-LoHuPohuQ3eUtDWL_G-" : {
"creationDate" : 1.567980367209054E9,
"text" : " Ok",
"uid" : "64r3dgTN6xMhHYhptFlsFWX0dLk2"
}
}
},

我不知道如何检索 autoId,以便当前登录的用户可以删除他们的评论。这是提交的代码


func didSubmit(for comment: String) {

guard let uid = Auth.auth().currentUser?.uid else { return }

print("post id:", self.post?.postId ?? "")

print("Inserting comment:", comment)


let postId = self.post?.postId ?? ""
let values = ["text": comment, "creationDate": Date().timeIntervalSince1970, "uid": uid] as [String : Any]

Database.database().reference().child("comments").child(postId).childByAutoId().updateChildValues(values) { (err, ref) in

if let err = err {
print("Failed to insert comment:", err)
return
}

self.uploadCommentNotificationToServer()

if comment.contains("@") {
self.uploadMentionNotification(forPostId: postId, withText: comment, isForComment: true)
}

self.containerView.clearCommentTextView()
}

}

注释结构


struct Comment {
var commentId: String!
let user: User
var creationDate: Date!
let text: String
let uid: String!

init(commentId: String!,user: User, dictionary: [String: Any]) {
self.commentId = commentId
self.user = user
self.text = dictionary["text"] as? String ?? ""
self.uid = dictionary["uid"] as? String ?? ""

if let creationDate = dictionary["creationDate"] as? Double {
self.creationDate = Date(timeIntervalSince1970: creationDate)
}

}

var post: Post?


func deleteComment() {
guard let postId = self.post?.postId else { return }

let commentsRef = Database.database().reference().child("comments")
commentsRef.child(postId).child(commentId).removeValue()




}




}


获取评论的代码


var comments = [Comment]()

func fetchComments() {
guard let postId = self.post?.postId else { return }
let ref = Database.database().reference().child("comments").child(postId)
ref.observe(.childAdded, with: { (snapshot) in
let commentId = snapshot.key
guard let dictionary = snapshot.value as? [String: Any] else { return }

guard let uid = dictionary["uid"] as? String else { return }

Database.fetchUserWithUID(with: uid, completion: { (user) in
let comment = Comment(commentId: commentId, user: user, dictionary: dictionary)

self.comments.append(comment)
self.collectionView?.reloadData()
})

}) { (err) in
print("Failed to observe comments")
}
}

谢谢!

最佳答案

要能够删除节点,您必须知道该节点的完整路径。

有两种方法可以知道要删除的产品的 key :

  1. 从加载数据的那一刻起,您就将它传递给您的应用。
  2. 您有一些其他值,允许您对数据库执行查询以查找键。

第一个选项是最常见的,因为您通常会从数据库中加载数据以将其显示给用户。在那种情况下,您应该“简单地”传递 key显示 value 时的数据.

一旦你有了产品/子节点的键,你就可以删除它:

let postId  = "-LmfZZis5ovtBwfm_4xR"
let commentId = "-LoHu5Qv3BmuHTsSlthj"
let commentsRef = Database.database().reference().child("comments")
commentsRef.child(postId).child(commentId).removeValue()

关于ios - 如何检索 autoId 以删除值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57846184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com