尝试从Firebase数据库中提取数据时获得nil响应

时间:2017-05-22 16:02:49

标签: ios swift firebase firebase-realtime-database

Corresponding Firebase database data structure. Trying to extract "text" for the most recent text message.以下代码尝试从上面看到的Firebase数据库数据结构中提取文本消息,但返回的快照值为nil而不起作用。另外,当我运行" hasChildren"函数,它返回false。 chatListingID使用chatListing(而不是消息)childByAutoID值正确填充。

lazy var chatRef: FIRDatabaseReference = FIRDatabase.database().reference().child("chatListings")

func configureChatListingCell(chatListing: ChatListing){
    chatListingID = chatListing.chatRoomKey
    chatRef.child(chatListingID).child("messages").child("MediaType").queryEqual(toValue: "TEXT").queryLimited(toLast: 1).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{
                lastMessage = dict["text"] as! String
                lastMessagerId = dict["senderId"] as! String
            }
            print("Most recent message is \(lastMessage) from user \(lastMessagerId).")
        }
    })

理论上,我应该返回一个包含最新短信和senderId的结果。这两个值是我感兴趣的。

0 个答案:

没有答案