使用查询从 firebase 获取新评论
Fetch new comments from firebase using a query
我有一个在用户刷新页面时调用的 handleRefresh() 函数。当刷新发生时,已发布的新评论将加载到表视图中。
我遇到的问题是,当用户刷新 tableview 中的数据时,加载了两次,所以我得到了更新的评论,但有重复的评论,它再次重新加载了旧评论。
我对如何解决这个问题有点困惑。
这是代码。
var CommentsQuery: DatabaseQuery {
let postRef = Database.database().reference().child("posts")
let postKey = keyFound
let postCommentRef = postRef.child(postKey)
let lastComment = self.comments.last
var queryRef: DatabaseQuery
if lastComment == nil {
queryRef = postCommentRef.queryOrdered(byChild: "timestamp")
} else {
let lastTimestamp = lastComment!.createdAt.timeIntervalSince1970 * 1000
queryRef = postCommentRef.queryOrdered(byChild: "timestamp").queryEnding(atValue: lastTimestamp)
}
return queryRef
}
@objc func handleRefresh() {
CommentsQuery.queryLimited(toLast: 20).observeSingleEvent(of: .value) { snapshot in
var tempComments = [Comments]()
let commentsSnap = snapshot.childSnapshot(forPath: "comments")
let allComments = commentsSnap.children.allObjects as! [DataSnapshot]
for commentSnap in allComments {
let degree = commentSnap.childSnapshot(forPath: "reply degree").value as? String ?? ""
let name = commentSnap.childSnapshot(forPath: "reply name").value as? String ?? ""
let text = commentSnap.childSnapshot(forPath: "reply text").value as? String ?? ""
let university = commentSnap.childSnapshot(forPath: "reply university").value as? String ?? ""
let photoURL = commentSnap.childSnapshot(forPath: "reply url").value as? String ?? ""
let url = URL(string: photoURL)
let timestamp = commentSnap.childSnapshot(forPath: "timestamp").value as? Double
let lastComment = self.comments.last
if snapshot.key == lastComment?.id {
let newComments = Comments(id: snapshot.key, fullname: name, commentText: text, university: university, degree: degree, photoURL: photoURL, url: url!, timestamp: timestamp!)
tempComments.insert(newComments, at: 0)
print("fetchRefresh")
}
}
self.comments.insert(contentsOf: tempComments, at: 0)
self.fetchingMore = false
self.refreshControl.endRefreshing()
self.tableView.reloadData()
}
}
如果 self.comments.last
在页面重新加载后仍然存在,那么问题似乎出在您在此处使用 queryEnding(atValue:
:
queryRef = postCommentRef.queryOrdered(byChild: "timestamp").queryEnding(atValue: lastTimestamp)
由于时间戳值是递增的(值越高越新),您希望节点的时间戳 比最新值高 ,您可以使用 queryStarting(atValue:
和不是 queryEnding(atValue:
.
我有一个在用户刷新页面时调用的 handleRefresh() 函数。当刷新发生时,已发布的新评论将加载到表视图中。
我遇到的问题是,当用户刷新 tableview 中的数据时,加载了两次,所以我得到了更新的评论,但有重复的评论,它再次重新加载了旧评论。
我对如何解决这个问题有点困惑。
这是代码。
var CommentsQuery: DatabaseQuery {
let postRef = Database.database().reference().child("posts")
let postKey = keyFound
let postCommentRef = postRef.child(postKey)
let lastComment = self.comments.last
var queryRef: DatabaseQuery
if lastComment == nil {
queryRef = postCommentRef.queryOrdered(byChild: "timestamp")
} else {
let lastTimestamp = lastComment!.createdAt.timeIntervalSince1970 * 1000
queryRef = postCommentRef.queryOrdered(byChild: "timestamp").queryEnding(atValue: lastTimestamp)
}
return queryRef
}
@objc func handleRefresh() {
CommentsQuery.queryLimited(toLast: 20).observeSingleEvent(of: .value) { snapshot in
var tempComments = [Comments]()
let commentsSnap = snapshot.childSnapshot(forPath: "comments")
let allComments = commentsSnap.children.allObjects as! [DataSnapshot]
for commentSnap in allComments {
let degree = commentSnap.childSnapshot(forPath: "reply degree").value as? String ?? ""
let name = commentSnap.childSnapshot(forPath: "reply name").value as? String ?? ""
let text = commentSnap.childSnapshot(forPath: "reply text").value as? String ?? ""
let university = commentSnap.childSnapshot(forPath: "reply university").value as? String ?? ""
let photoURL = commentSnap.childSnapshot(forPath: "reply url").value as? String ?? ""
let url = URL(string: photoURL)
let timestamp = commentSnap.childSnapshot(forPath: "timestamp").value as? Double
let lastComment = self.comments.last
if snapshot.key == lastComment?.id {
let newComments = Comments(id: snapshot.key, fullname: name, commentText: text, university: university, degree: degree, photoURL: photoURL, url: url!, timestamp: timestamp!)
tempComments.insert(newComments, at: 0)
print("fetchRefresh")
}
}
self.comments.insert(contentsOf: tempComments, at: 0)
self.fetchingMore = false
self.refreshControl.endRefreshing()
self.tableView.reloadData()
}
}
如果 self.comments.last
在页面重新加载后仍然存在,那么问题似乎出在您在此处使用 queryEnding(atValue:
:
queryRef = postCommentRef.queryOrdered(byChild: "timestamp").queryEnding(atValue: lastTimestamp)
由于时间戳值是递增的(值越高越新),您希望节点的时间戳 比最新值高 ,您可以使用 queryStarting(atValue:
和不是 queryEnding(atValue:
.