使用Firebase-iOS观察实时聊天数据时应用程序崩溃

时间:2018-05-24 03:14:54

标签: ios swift firebase chat jsqmessagesviewcontroller

我目前正在学习Swift,我决定使用iOS制作一个Firebase消息传递应用。我使用JSQMessageViewController作为我的聊天模板,一切正常,除了当两个用户同时在聊天室中聊天时应用程序崩溃的事实。我在下面的函数底部附近收到此错误:"线程1:EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0)"

这是我的观察和检索消息数据的代码。每当视图出现时我都会这样说:

private func observeMessages() {
    messageRef = ref.child("ChatRooms").child(chatRoomId!).child("Messages")

    let messageQuery = messageRef.queryLimited(toLast:25)

    newMessageRefHandle = messageQuery.observe(.childAdded, with: { (snapshot) in

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


        if  let data        = snapshot.value as? [String: AnyObject],
            let id          = data["sender_id"] as? String,
            let name        = data["name"] as? String,
            let text        = data["text"] as? String,
            let time        = data["time"] as? TimeInterval,
            !text.isEmpty
        {
            if id != uid! {
                let updateRead  = ref.child("ChatRooms").child(self.chatRoomId!).child("Messages").child(snapshot.key)
                updateRead.updateChildValues(["status":"read"])
            }
            if let message = JSQMessage(senderId: id, senderDisplayName: name, date: Date(timeIntervalSince1970: time), text: text)
            {
                self.messages.append(message)

                self.finishReceivingMessage()
            }

        }else if let id = messageData["senderId"] as! String!,
            let photoURL = messageData["photoURL"] as! String! { // 1

            if let mediaItem = JSQPhotoMediaItem(maskAsOutgoing: id == self.senderId) {

                self.addPhotoMessage(withId: id, key: snapshot.key, mediaItem: mediaItem)

                if photoURL.hasPrefix("gs://") {
                    self.fetchImageDataAtURL(photoURL, forMediaItem: mediaItem, clearsPhotoMessageMapOnSuccessForKey: nil)
                }
            }
        }else {
            print("Error! Could not decode message data")
        }
    })
    updatedMessageRefHandle = messageRef.observe(.childChanged, with: { (snapshot) in
        let key = snapshot.key

        //I am getting an error on this line 
        let messageData = snapshot.value as! Dictionary<String, String> 

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

好奇我在这里可能做错了什么。感谢所有帮助!

0 个答案:

没有答案