Reputation:
I am running the code to load the chats in swift iOS chat application. The problem in chat application is at "query in else{}" in function paginateData() while running the app, the first "query in if{}" runs smoothly as expected, but in other one does not run. I am using firestore database for implementing chat. My goal is to paginate the chats in the chat room. If any more information is required, please let me know.
CODE
func paginateData() {
fetchingMore = true
var query: Query!
if messages.isEmpty {
query = Firestore.firestore().collection("messageRoom").document(messageRoomIdFinal).collection("messages").order(by: "timestamp", descending: false).limit(to: 20)
print("First 20 Messages loaded")
} else {
query = Firestore.firestore().collection("messageRoom").document(messageRoomIdFinal).collection("messages").order(by: "timestamp", descending: false).start(afterDocument: lastDocumentSnapshot).limit(to: 7)
print("Next 7 Messages loaded")
}
query.addSnapshotListener { (snapshot, err) in
if let err = err {
print("\(err.localizedDescription)")
} else if snapshot!.isEmpty {
self.fetchingMore = false
return
} else {
snapshot!.documentChanges.forEach { diff in
if (diff.type == .added) {
let snap = diff.document
let aMessage = Message(withSnap: snap)
self.messages.append(aMessage)
DispatchQueue.main.async {
self.collectionView.reloadData()
let indexPath = IndexPath(item: self.messages.count - 1, section: 0)
self.collectionView.scrollToItem(at: indexPath, at: .bottom, animated: true)
}
}
if (diff.type == .modified) {
let docId = diff.document.documentID
DispatchQueue.main.async {
self.collectionView.reloadData()
let indexPath = IndexPath(item: self.messages.count - 1, section: 0)
self.collectionView.scrollToItem(at: indexPath, at: .bottom, animated: true)
}
//update the message with this documentID in the array
}
if (diff.type == .removed) {
let docId = diff.document.documentID
//remove the message with this documentID from the array
}
self.lastDocumentSnapshot = snapshot!.documents.last
}
}
}
}
Upvotes: 2
Views: 890
Reputation: 35658
Unrelated to the question but UI calls within Firebase closures are run on the main thread so you can remove the DispatchQueue.
I don't think your code is very far off. I re-wrote it to paginate loading users 3 at a time by age and the below code works correctly.
Take a look and compare with your code. Each time this is called, it loads in the next three users.
var lastDocumentSnapshot: DocumentSnapshot?
func observeUsersWithPagination() {
var query: Query!
let usersCollectionRef = self.db.collection("users")
if let nextStartingSnap = self.lastDocumentSnapshot {
query = usersCollectionRef.order(by: "age", descending: false).start(afterDocument: nextStartingSnap).limit(to: 3)
} else {
query = usersCollectionRef.order(by: "age", descending: false).limit(to: 3)
}
query.addSnapshotListener { querySnapshot, error in
guard let snapshot = querySnapshot else {
print("Error fetching snapshots: \(error!)")
return
}
self.lastDocumentSnapshot = snapshot.documents.last
snapshot.documentChanges.forEach { diff in
let userName = diff.document.get("name") as? String ?? "No Name"
let age = diff.document.get("age") as? Int ?? 0
if (diff.type == .added) {
print("Added user: \(userName)", age)
}
if (diff.type == .modified) {
print("Modified user: \(userName)", age)
}
if (diff.type == .removed) {
print("Removed user: \(userName)", age)
}
}
}
}
It's not clear if documentChanges.forEach is really needed.
Upvotes: 3