聊天分页中面临的问题

编程入门 行业动态 更新时间:2024-10-21 19:53:59
本文介绍了聊天分页中面临的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在运行代码以在快速的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.

更多推荐

聊天分页中面临的问题

本文发布于:2023-11-28 13:07:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642532.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:分页

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!