The following code attempts to extract a text message from the Firebase database data structure described above, but instead returns snapshot.value nil and does not work. Also, when I run the hasChildren function, it returns false. In the chatListingID file, the childByAutoID value for the chatListing (not Messages) parameter is correctly populated.
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).")
}
})
Theoretically, I should return one result with the most recent text message and senderId. I am interested in those two values.
source
share