发送邮件后发生崩溃

时间:2018-01-30 10:48:14

标签: ios swift firebase

一切正常,然后我从My 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
            }
        }
    })

}

1 个答案:

答案 0 :(得分:2)

错误很可能是强制施放的结果 - as!

而不是

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

DO

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

您的snapshot.valuenil,或者不是Dictionary<String, String>的实例,强制将其投射到此类会导致崩溃。

你还应该阅读更多关于选项和Swift中的类型转换的内容,因为你经常使用!,而且你的程序中没有一次正确使用它。