'SwiftUI Firebase Pagination Order Users by id
I have a page for users in a grid view and would like to paginate the view to reduce bandwidth when a user scrolls. For now i know how to paginate a feed view but cannot for a user grid view. Below is my code and what i am trying to achieve.
This is the code i use to paginate a feed view:
func fetchUserPosts() {
let query = Firestore.firestore().collection("posts").limit(to: 15).order(by: "timestamp", descending: true)
if let last = lastDoc {
let next = query.start(afterDocument: last)
next.getDocuments { snapshot, _ in
guard let documents = snapshot?.documents, !documents.isEmpty else { return }
self.lastDoc = snapshot?.documents.last
self.posts.append(contentsOf: documents.compactMap({ try? $0.data(as: Post.self) }))
}
} else {
query.getDocuments { snapshot, _ in
guard let documents = snapshot?.documents else { return }
self.posts = documents.compactMap({ try? $0.data(as: Post.self) })
self.lastDoc = snapshot?.documents.last
}
}
}
I am trying to achieve the same for a usergrid view, below is how far i have gone with errors i have encountered. Any help is appreciated.
class PeopleViewModel: ObservableObject {
@Published var users = [User]()
func fetchAllUsers() {
let query = Firestore.firestore().collection("users").limit(to: 20).order(by: "id", descending: true)
if let last = lastDoc {
let next = query.start(afterDocument: last)
next.getDocuments { snapshot, _ in
guard let documents = snapshot?.documents, !documents.isEmpty else { return }
self.lastDoc = snapshot?.documents.last
self.users //Property is accessed but result is unused
}
} else {
query.getDocuments { snapshot, _ in
guard let documents = snapshot?.documents else { return }
self.users //Property is accessed but result is unused
self.lastDoc = snapshot?.documents.last
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
