Ios 如何在处使用重新加载项,而不是使用firestore重新加载所有数据
我有一个使用Firestore的聊天应用程序,当一个新的Ios 如何在处使用重新加载项,而不是使用firestore重新加载所有数据,ios,swift,google-cloud-firestore,uicollectionview,Ios,Swift,Google Cloud Firestore,Uicollectionview,我有一个使用Firestore的聊天应用程序,当一个新的文档或->(消息)添加到Firestore my collectionView时,会重新加载所有数据,并且应用程序会冻结一秒钟,直到重新加载所有数据,我不太了解collectionView,但我认为如果我没有错的话,这是因为collectionView试图计算每个单元格的大小,这是调试器消息 The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlo
文档或->(消息)
添加到Firestore my collectionView时,会重新加载所有数据,并且应用程序会冻结一秒钟,直到重新加载所有数据,我不太了解collectionView,但我认为如果我没有错的话,这是因为collectionView试图计算每个单元格的大小,这是调试器消息
The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x7fbd974b3c40>, and it is attached to <UICollectionView: 0x7fbd97930600; frame = (0 61; 414 663); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x600002c5df20>; layer = <CALayer: 0x6000023f9640>; contentOffset: {-5, -5}; contentSize: {404, 8}; adjustedContentInset: {5, 5, 5, 5}; layout: <UICollectionViewFlowLayout: 0x7fbd974b3c40>; dataSource: <DELEVARE___ديليفري.DriverChat: 0x7fbd97843600>>.
2021-01-16 13:54:43.232052+0200 DELEVARE - ديليفري[51104:999159] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.
这是我的代码
我对自己编码很陌生。 然而,在我看来,有一个
.onSnapshot
侦听器可以帮助您查找添加的类型的更改
我发现本文对我正在开发的CRUD应用程序非常有用:
它的一部分展示了如何监听各种类型的更改。
我能够在我的应用程序中实现它,只返回添加的文档而不是整个集合,然后将其附加到我的数组中
tutorialsRef.onSnapshot(function(snapshot) {
snapshot.docChanges().forEach(function(change) {
if (change.type === "added") {
console.log("New tutorial: ", change.doc.data());
}
if (change.type === "modified") {
console.log("Modified tutorial: ", change.doc.data());
}
if (change.type === "removed") {
console.log("Removed tutorial: ", change.doc.data());
}
});
});
这意味着,如果我在viewDidload上调用此方法,这意味着在发生新更改之前,它将无法工作??而且它不会加载任何数据??老实说,我不太明白你在做什么。但是对于我的应用程序,我能够让onSnapshot
使用.docChanges()
监听更改。我想如果我理解正确,这将返回一个对象,其中包括更改类型-change.type
-更改文档的id-change.doc.id
-以及文档的完整内容-change.doc.data()
。然后我检查这个doc.id是否已经处于我的状态,如果没有,我将该文档添加到我的状态对象数组中。这可能会对您有所帮助,因为.onSnapshot
仅获取对Firestore的更改。。。我想。
func getMessages(){
guard let uid = Auth.auth().currentUser?.uid else {return}
let firestore = Firestore.firestore()
let doc = firestore.collection("طلبات قيد العمل").whereField("driverUid", isEqualTo: uid)
doc.getDocuments { (query, err) in
if err != nil {
print(err?.localizedDescription ?? "")
}
for document in query!.documents{
let chatDoc = document.reference.collection("المحادثات")
chatDoc.addSnapshotListener { (querysnap, err) in
if err != nil {
print(err?.localizedDescription ?? "")
}
self.messages = []
for snap in querysnap!.documents{
let data = snap.data()
guard let message = data["message"] as? String else {return}
guard let senderid = data["senderID"] as? String else {return}
guard let timestamp = data["date"] as? Timestamp else {return}
let date = Date(timeIntervalSince1970: TimeInterval(timestamp.seconds))
if senderid == Auth.auth().currentUser?.uid{
let drivermessages = driverMessages(image: UIImage(), text: message, date: date, isSender: true, driverid: senderid)
self.messages.append(drivermessages)
}else {
guard let message2 = data["message"] as? String else {return}
guard let senderid2 = data["senderID"] as? String else {return}
guard let timestamp2 = data["date"] as? Timestamp else {return}
guard let img = self.profileImg else {return}
let date2 = Date(timeIntervalSince1970: TimeInterval(timestamp2.seconds))
let usermessages = driverMessages(image: img, text: message2, date: date2, isSender: false, driverid: senderid2)
self.messages.append(usermessages)
}
DispatchQueue.main.async {
self.collectionView.reloadData()
let item = self.messages.count - 1
let insertionIndexPath = IndexPath(item: item, section: 0)
self.collectionView.scrollToItem(at: insertionIndexPath, at: .bottom, animated: true)
}
}
}
}
}
}
tutorialsRef.onSnapshot(function(snapshot) {
snapshot.docChanges().forEach(function(change) {
if (change.type === "added") {
console.log("New tutorial: ", change.doc.data());
}
if (change.type === "modified") {
console.log("Modified tutorial: ", change.doc.data());
}
if (change.type === "removed") {
console.log("Removed tutorial: ", change.doc.data());
}
});
});