Getting a response when trying to retrieve data from a Firebase database

Corresponding Firebase database data structure.  Attempt to extractThe 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.

+4
source share

Source: https://habr.com/ru/post/1677705/


All Articles