在聊天集合的Firestore中,有几个文档:
文档1:
receiverID:“Leo”
消息:“你好”
...
文档2:
receiverID:“彼得”
消息:“谢谢你帮助我”
...
文件3:
receiverID:“Leo”
消息:“我很高兴帮助你进入堆栈溢出”
在我创建收件箱时,目前它正在向我展示用户的集合视图,例如“Leo”,“Peter”,“Leo”。我想在我的收件箱中只显示Leo和Peter这样的独特用户,如何进行客户端过滤以实现结果?
以下是我在InboxViewController中的代码
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
return chats.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! InboxCollectionViewCell
cell.inboxMessage.text = chats[indexPath.row].receiverID
return cell
}
func loadPosts() {
let senderIDNumber = Auth.auth().currentUser?.uid
let chatsRef = db.collection("chats")
chatsRef.whereField("senderID", isEqualTo: senderIDNumber!)
.addSnapshotListener { querySnapshot, error in
guard let documents = querySnapshot?.documents else {
print("Error fetching documents: \(error!)")
return
}
for document in documents {
let messageText = document.data()["message"] as? String
let senderIDNumber = document.data()["senderID"] as? String
let receiverIDNumber = document.data()["receiverID"] as? String
let timestamp = document.data()["timestamp"] as? String
let conversationsCounter = document.data()["conversationsCounter"] as? Int
let chat = Chat(messageTextString: messageText!, senderIDNumber: senderIDNumber!, receiverIDNumber: receiverIDNumber!, timeStampString: timestamp!, conversationsCounterInt: conversationsCounter!)
self.chats.append(chat)
print(self.chats)
self.collectionView.reloadData()
}
}
}
}
目前,我在收件箱中收到此信息:
狮子座
彼得
利奥
我只想看到名字
狮子座
彼得
删除重复项。
答案 0 :(得分:0)
在聊天中检查是否存在receiveid
if chats.contains(where: { $0. receiverIDNumber == receiverIDNumber }) {
{
}else{
let chat = Chat(messageTextString: messageText!, senderIDNumber: senderIDNumber!, receiverIDNumber: receiverIDNumber!, timeStampString: timestamp!, conversationsCounterInt: conversationsCounter!)
self.chats.append(chat)
}