Monday, March 27, 2023
HomeiOS Developmentios - cannot learn from firebase realtime database

ios – cannot learn from firebase realtime database


I’ve firebase database applied, I need to parse all of the json knowledge which is added by me in realtime database. code under symbolize how I attempt to parse it, however the error is all the time the identical it is: Didn't parse conversations which is snapshot.worth error as you possibly can see within the code

    public func getConversation(with electronic mail: String, completion: @escaping(End result<Dialog, Error>) -> Void) {
        database.little one("(electronic mail)/conversations").observeSingleEvent(of: .worth) { snapshot in
            guard let worth = snapshot.worth as? [[String: Any]] else {
                print("Didn't parse snapshot worth")
                let error = NSError(area: "", code: 0, userInfo: [NSLocalizedDescriptionKey: "Failed to parse conversations"])
                completion(.failure(error))
                return
            }
            
            let conversations: [Conversation] = worth.compactMap { dictionary in
                guard let conversationId = dictionary["id"] as? String else {
                    print("Didn't parse dialog ID")
                    return nil
                }

                guard let latestMessage = dictionary["latest_message"] as? [String: Any],
                      let message = latestMessage["message"] as? String,
                      let date = latestMessage["date"] as? String,
                      let isRead = latestMessage["is_read"] as? Int else {
                    print("Didn't parse newest message")
                    return nil
                }

                guard let title = dictionary["name"] as? String,
                      let otherUserEmail = dictionary["other_user_email"] as? String else {
                    print("Didn't parse title or different person electronic mail")
                    return nil
                }

                let latestMessageObject = LatestMessage(date: date, isRead: isRead, message: message)
                return Dialog(id: conversationId, latestMessage: latestMessageObject, title: title, otherUserEmail: otherUserEmail)
            }
     }
}

within the code under, that is how I name that perform which is in one other viewcontroller
the error is from right here case .failure(let error), there isn’t any method the e-mail is incorrect, the safeEmail is what I count on to be.

    personal func startListeningForConversations(){
        guard let electronic mail = UserDefaults.commonplace.worth(forKey: "electronic mail") as? String else {
            return
        }
        let safeEmail = DatabaseManager.safeEmail(electronic mail: electronic mail)
        print(safeEmail)
        DatabaseManager.shared.getConversation(with: safeEmail) { [weak self] end in
            swap end result {
// DatabaseManager is the controller the place getConversation perform is situated
            case .success(let conversations):
                print(conversations)
            case .failure(let error):
                print("Error fetching dialog: (error.localizedDescription)")
            }
        }
    }

I attempted totally different strategies to parse, however the error is similar

code under is what I get within the console after printin snapshot.worth in getConversation perform

Optionally available({
    id = "conversation_test-mail-ru_test-mail-ru_Mar 1, 2023 at 1:51:21 AM GMT+4";
    "latest_message" =     {
        date = "Mar 1, 2023 at 1:51:21 AM GMT+4";
        "is_read" = 0;
        message = Asdasdasda;
    };
    title = levani;
    "other_user_email" = "test-mail-ru";
})

however the error is Didn't parse conversations

this is screenshot of my realtime database:

enter image description here

I even deleted and redownloaded googleservice-info file, however nonetheless cannot parse.
any answer might be appericated

I made isRead Int as a result of ase you possibly can see within the console log firebase returns 0 as a substitute of false

struct Dialog {
    let id: String
    let latestMessage: LatestMessage
    let title: String
    let otherUserEmail: String
}
struct LatestMessage {
    let date: String
    let isRead: Int
    let message: String
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments