gpt4 book ai didi

ios - 如何检索评论/帖子的键

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

我很难尝试同时检索 postId 和 commentId。

目标是允许用户删除自己对帖子的评论。

这是 Comment 结构,下面是一个应该删除用户评论的函数,但是由于某些原因我只能返回 commentId 而不是 postId。所以点击时评论不会被删除。

如果我在 deleteComment 的最后一行运行一个断点,postId 将 = nil,但是如果我在 func fetchComment 的最后一行运行一个断点postId 将返回帖子的 postId。


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 }
guard let commentId = self.commentId 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
//print(commentId)
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")
}

}

另外,这里是用户提交评论时的代码


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()
}

}

Json 通过 firebase 进行评论

  "comments" : {
"-Lord0UWPkh5YdGIHtAO" : {
"-Lp7AQzHccme5RcRsyDd" : {
"creationDate" : 1.568874020882821E9,
"text" : "Wow Cool!",
"uid" : "wELwYnMGxxW0LJNRKBuuE2BUaV93"
}
},
"-LowvCk-agvJbK0VF-Bq" : {
"-LpKm1NcgOsXhwj6soxE" : {
"creationDate" : 1.569102243436777E9,
"text" : "Nice!",
"uid" : "wELwYnMGxxW0LJNRKBuuE2BUaV93"
},

最佳答案

一种选择是将 postId 设为 Comment 结构中的一个属性,并将其添加到 init 方法中,这样您将始终可以访问它

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

init(commentId: String!,user: User, postIdentifier: String, dictionary: [String: Any]) {
self.commentId = commentId
self.user = user
self.postId = postIdentifier
...

关于ios - 如何检索评论/帖子的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58253809/

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