我正在运行代码以在快速的iOS聊天应用程序中加载聊天.聊天应用程序中的问题是在运行应用程序时在函数paginateData()中的"query in else {}"中运行,第一个"if {}中的查询"按预期运行得很顺利,而其他应用程序则没有运行.我正在使用Firestore数据库来实现聊天. 我的目标是在聊天室中对聊天进行分页.如果需要更多信息,请告诉我.
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 } } } }推荐答案
与问题无关,但Firebase闭包中的UI调用在主线程上运行,因此您可以删除DispatchQueue.
Unrelated to the question but UI calls within Firebase closures are run on the main thread so you can remove the DispatchQueue.
我认为您的代码距离还很遥远.我重新编写了它,以按年龄分次加载用户3分页,下面的代码可以正常工作.
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) } } } }尚不清楚是否真的需要documentChanges.forEach.
It's not clear if documentChanges.forEach is really needed.
更多推荐
聊天分页中面临的问题
发布评论