以下是我的viewDidAppear函数,该函数仅在用户第一次打开聊天时成功加载所有先前的消息。之后,当用户退出聊天然后启动另一个聊天时,来自上一个聊天的消息在顶部加载,最新聊天的特定消息被添加到这些消息的末尾。显然不理想。我单击chatVC的后退按钮退出时尝试从messages数组中删除所有消息(如下所示),但这会导致应用程序在尝试加载新聊天时崩溃。具体来说,它在override func collectionView(_ collectionView:JSQMessagesCollectionView!,messageDataForItemAt indexPath:IndexPath!) - >上崩溃了。 JSQMessageData! { 返回消息[indexPath.item] }(也见下文)。
override func viewDidAppear(_ animated: Bool) {
if presentedChatVCFromChatListVC{
chatRef.child(chatListingID).child("messages").observe(.value, with: { snapshot in
print("snapshot.value is \(snapshot.value)")
for child in snapshot.children {
let childData = child as! FIRDataSnapshot
print("childData is \(childData)")
if let dict = childData.value as? NSDictionary{
let mediaType = dict["MediaType"] as! String
let senderId = dict["senderId"] as! String
let senderName = dict["senderName"] as! String
self.observeUsers(senderId)
switch mediaType {
case "TEXT":
let text = dict["text"] as! String
self.messages.append(JSQMessage(senderId: senderId, displayName: senderName, text: text))
self.collectionView.reloadData()
case "PHOTO":
let photo = JSQPhotoMediaItem(image: nil)
let fileUrl = dict["fileUrl"] as! String
let downloader = SDWebImageDownloader.shared()
downloader.downloadImage(with: URL(string: fileUrl)!, options: [], progress: nil, completed: { (image, data, error, finished) in
DispatchQueue.main.async(execute: {
photo?.image = image
self.collectionView.reloadData()
})
})
self.messages.append(JSQMessage(senderId: senderId, displayName: senderName, media: photo))
if self.senderId == senderId {
photo?.appliesMediaViewMaskAsOutgoing = true
}else{
photo?.appliesMediaViewMaskAsOutgoing = false
}
case "VIDEO":
let fileUrl = dict["fileUrl"] as! String
let video = URL(string: fileUrl)!
let videoItem = JSQVideoMediaItem(fileURL: video, isReadyToPlay: true)
self.messages.append(JSQMessage(senderId: senderId, displayName: senderName, media: videoItem))
if self.senderId == senderId {
videoItem?.appliesMediaViewMaskAsOutgoing = true
} else {
videoItem?.appliesMediaViewMaskAsOutgoing = false
}
default:
print("unknown data type")
}
}
self.collectionView.reloadData()
}
})
}else{
chatRef.queryOrdered(byChild: "otherUserID").queryEqual(toValue: otherUID).observe(.value, with:{
snapshot in
print("snapshot.value is \(snapshot.value)")
for child in snapshot.children {
let childData = child as! FIRDataSnapshot
print("childData is \(childData)")
if let dict = childData.value as? NSDictionary{
let temp = dict["userID"] as! String
if temp == userID{
chatListingID = dict["chatRoomKey"] as! String
self.chatRef.child(chatListingID).child("messages").observe(.value, with:{
snapshot in
for child1 in snapshot.children {
let childData1 = child1 as! FIRDataSnapshot
print("childData1 is \(childData1)")
if let dict1 = childData1.value as? NSDictionary{
let mediaType = dict1["MediaType"] as! String
let senderId = dict1["senderId"] as! String
let senderName = dict1["senderName"] as! String
self.observeUsers(senderId)
switch mediaType {
case "TEXT":
let text = dict1["text"] as! String
self.messages.append(JSQMessage(senderId: senderId, displayName: senderName, text: text))
self.collectionView.reloadData()
case "PHOTO":
let photo = JSQPhotoMediaItem(image: nil)
let fileUrl = dict1["fileUrl"] as! String
let downloader = SDWebImageDownloader.shared()
downloader.downloadImage(with: URL(string: fileUrl)!, options: [], progress: nil, completed: { (image, data, error, finished) in
DispatchQueue.main.async(execute: {
photo?.image = image
self.collectionView.reloadData()
})
})
self.messages.append(JSQMessage(senderId: senderId, displayName: senderName, media: photo))
if self.senderId == senderId {
photo?.appliesMediaViewMaskAsOutgoing = true
}else{
photo?.appliesMediaViewMaskAsOutgoing = false
}
case "VIDEO":
let fileUrl = dict1["fileUrl"] as! String
let video = URL(string: fileUrl)!
let videoItem = JSQVideoMediaItem(fileURL: video, isReadyToPlay: true)
self.messages.append(JSQMessage(senderId: senderId, displayName: senderName, media: videoItem))
if self.senderId == senderId {
videoItem?.appliesMediaViewMaskAsOutgoing = true
} else {
videoItem?.appliesMediaViewMaskAsOutgoing = false
}
default:
print("unknown data type")
}
}
}
})
}
}
self.collectionView.reloadData()
}
})
}
observeTyping()
}
override func collectionView(_ collectionView: JSQMessagesCollectionView!, messageDataForItemAt indexPath: IndexPath!) -> JSQMessageData! {
return messages[indexPath.item]
}
func backButtonDidClick(){
self.messages.removeAll()
dismiss(animated: true, completion: nil)
}
此外,在“self.messages.removeAll()”之后添加“self.collectionView.reloadData()”会导致错误地进入另一个聊天,开始为空。比崩溃好一点,但并不多。如何清除消息,以便更改用户消息的结果导致聊天视图控制器仅加载该用户的消息,而不是之前加载的消息或根本不加载的消息?
答案 0 :(得分:0)
这是因为你已将所有逻辑放在viewDidAppear
这个方法中,并不像你在这里那样经常调用。所以当你导航到其他地方时,视图没有被破坏,当你回到它时会更快。你应该把这个逻辑移到另一个函数中。只放置设置视图的viewDidAppear
中的内容。设置背景颜色。您可能希望此逻辑位于viewDidLoad()
中,以便分配更多您正在寻找的行为。