gpt4 book ai didi

ios - 发送消息后崩溃

转载 作者:行者123 更新时间:2023-11-28 12:11:04 24 4
gpt4 key购买 nike

一切正常,然后我从我的 Firebase 数据库中删除了一些旧消息和对话。现在每次我发送消息时都会崩溃。我删除了所有旧用户并创建了新用户并尝试发送消息,但我仍然不断崩溃。我不确定是什么原因造成的。任何建议都会有所帮助。它首先发生在我测试了这个删除表格单元格的功能之后......

func deleteConversation(_ conversation:Conversation) {
guard let user = Auth.auth().currentUser else { return }
let ref = Database.database().reference()

let obj = [
"conversations/users/\(user.uid)/\(conversation.partner_uid)/muted": true
] as [String:Any]
print("OBBJ: \(obj)")
ref.updateChildValues(obj, withCompletionBlock: { error, ref in
if error != nil {
let alert = UIAlertController(title: "Error deleting conversation!", message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Okay", style: .default, handler: nil))
} else {
let alert = UIAlertController(title: "Conversation deleted!", message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Okay", style: .default, handler: nil))
}
})

enter image description here enter image description here

    func downloadMessages() {

self.messages = []

downloadRef?.observe(.childAdded, with: { snapshot in
let dict = snapshot.value as! [String:AnyObject]



if let sender = dict["sender"] as! String!, let recipient = dict["recipient"] as! String!, let text = dict["text"] as! String!, text.characters.count > 0 {

let timestamp = dict["timestamp"] as! Double

let date = NSDate(timeIntervalSince1970: timestamp/1000)

let message = JSQMessage(senderId: sender, senderDisplayName: "", date: date as Date!, text: text)
self.messages.append(message!)
self.reloadMessagesView()
self.finishReceivingMessage(animated: true)
}
else if let id = dict["sender"] as! String!,
let photoURL = dict["imageUrl"] as! String!, photoURL.characters.count > 0 { // 1
// 2
if let mediaItem = JSQPhotoMediaItem(maskAsOutgoing: id == self.senderId) {
// 3
let timestamp = dict["timestamp"] as! Double

let date = NSDate(timeIntervalSince1970: timestamp/1000)

if let message = JSQMessage(senderId: id, senderDisplayName: "", date: date as Date!, media: mediaItem) {
self.messages.append(message)

if (mediaItem.image == nil) {
self.photoMessageMap[snapshot.key] = mediaItem
}
self.collectionView.reloadData()
}
if photoURL.hasPrefix("gs://") {
self.fetchImageDataAtURL(photoURL, forMediaItem: mediaItem, clearsPhotoMessageMapOnSuccessForKey: nil)
}
}
}
else {
print("Error! Could not decode message data")
}
})

// We can also use the observer method to listen for
// changes to existing messages.
// We use this to be notified when a photo has been stored
// to the Firebase Storage, so we can update the message data
updatedMessageRefHandle = downloadRef?.observe(.childChanged, with: { (snapshot) in
let key = snapshot.key
let messageData = snapshot.value as! Dictionary<String, String> // 1

if let photoURL = messageData["imageUrl"] as String! { // 2
// The photo has been updated.
if let mediaItem = self.photoMessageMap[key] { // 3
self.fetchImageDataAtURL(photoURL, forMediaItem: mediaItem, clearsPhotoMessageMapOnSuccessForKey: key) // 4
}
}
})

}

最佳答案

错误很可能是强制转换的结果 - as!

代替

let messageData = snapshot.value as! Dictionary<String, String>

guard let messageData = snapshot.value as? Dictionary<String, String> else { return }  

你的 snapshot.valuenil , 或者不是 Dictionary<String, String> 的实例,并将其强制转换为这样会导致崩溃。

您还应该阅读更多有关 Swift 中的可选值和类型转换的信息,因为您使用 !很多,而且在您的程序中没有一次被正确使用。

关于ios - 发送消息后崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48519552/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com