Swift “我想加载更多”;评论“;当滚动到底部时,除非没有更多的;评论“;滚动
这些是我目前正在使用的函数,但它们首先加载一些注释,当用户滚动到底部时,它们会加载更多的注释,但随后它会重新开始,这是一种无限时间线的事情,我希望它在没有更多注释加载时停止Swift “我想加载更多”;评论“;当滚动到底部时,除非没有更多的;评论“;滚动,swift,firebase,firebase-realtime-database,Swift,Firebase,Firebase Realtime Database,这些是我目前正在使用的函数,但它们首先加载一些注释,当用户滚动到底部时,它们会加载更多的注释,但随后它会重新开始,这是一种无限时间线的事情,我希望它在没有更多注释加载时停止 extension CommentsViewController: UITableViewDelegate { func scrollViewDidScroll(_ scrollView: UIScrollView) { if scrollView.contentOffset.y + self
extension CommentsViewController: UITableViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentOffset.y + self.view.frame.size.height >= scrollView.contentSize.height {
loadMore()
}
}
}
func loadMore() {
guard !isLoading else {
return
}
isLoading = true
guard let latestCommentTimestamp = self.comments.last?.dateCreated else {
isLoading = false
return
}
Api.comment.getOldComments(withPostId: selectedPost!.pid!, start: latestCommentTimestamp, limit: 6) { (results) in
if results.count == 0 {
return
}
for result in results {
self.comments.append(result.0)
self.users.append(result.1)
}
self.isLoading = false
self.commentTableView.reloadData()
}
}
func getOldComments(withPostId id: String, start timestamp: Int, limit: UInt, completed: @escaping ([(Comment, UserModel)]) -> Void) {
let commentOrderQuery = K.DATABASE_POSTCOMM_REF.child(id).queryOrdered(byChild: "dateCreated")
let commentLimitedQuery = commentOrderQuery.queryEnding(atValue: timestamp - 1, childKey: "dateCreated").queryLimited(toLast: limit)
commentLimitedQuery.observeSingleEvent(of: .value) { (snapshot) in
let items = snapshot.children.allObjects as! [DataSnapshot]
let myGroup = DispatchGroup()
var results: [(comment: Comment, user: UserModel)] = []
for (_, item) in items.enumerated() {
myGroup.enter()
Api.comment.observeComment(withId: item.key) { (comment) in
Api.user.observeUser(withId: comment.uid!) { (user) in
if comment.uid! == user.uid {
results.append((comment, user))
myGroup.leave()
}
}
}
}
myGroup.notify(queue: .main) {
results.sort(by: {$0.0.dateCreated! > $1.0.dateCreated!})
completed(results)
}
}
}
func getRecentComments(withPostId id: String, start timestamp: Int? = nil, limit: UInt, completed: @escaping ([(Comment, UserModel)]) -> Void) {
var commentQuery = K.DATABASE_POSTCOMM_REF.child(id).queryOrdered(byChild: "dateCreated")
if let latestCommentTimestamp = timestamp, latestCommentTimestamp > 0 {
commentQuery = commentQuery.queryStarting(atValue: latestCommentTimestamp + 1, childKey: "dateCreated").queryLimited(toLast: limit)
} else {
commentQuery = commentQuery.queryLimited(toLast: limit)
}
var results: [(comment: Comment, user: UserModel)] = []
commentQuery.observeSingleEvent(of: .value) { (snapshot) in
let items = snapshot.children.allObjects as! [DataSnapshot]
let myGroup = DispatchGroup()
for (_, item) in items.enumerated() {
myGroup.enter()
Api.comment.observeComment(withId: item.key) { (comment) in
Api.user.observeUser(withId: comment.uid!) { (user) in
if comment.uid! == user.uid {
results.append((comment, user))
myGroup.leave()
}
}
}
}
myGroup.notify(queue: .main) {
results.sort(by: {$0.0.dateCreated! > $1.0.dateCreated!})
completed(results)
}
}
}
在viewDidLoad()中调用此函数
我在commentViewController中的变量也许我忽略了它,但我看不到注释的位置。首先。dateCreated正在更新;e、 g.
getRecentComments
似乎每次都从同一个起始位置开始。
func loadComments() {
isLoading = true
Api.comment.getRecentComments(withPostId: selectedPost!.pid!, start: comments.first?.dateCreated, limit: 10) { (results) in
if results.count > 0 {
results.forEach { (result) in
self.comments.append(result.0)
self.users.append(result.1)
}
}else {
self.noInfoLabel.isHidden = false
}
self.isLoading = false
self.commentTableView.reloadData()
}
}
var selectedPost: Post?
var comments = [Comment]()
var users = [UserModel]()
var isLoading = false