how to get latest message from firebase database in ios

Gaurav Gupta

I have don't know what the problem please help me. When I get a particular message from firebase database then the value is getting but my app got a crash on one line.So please tell me what I do wrong in my code.below is my function.We also provide the screenshot of the error.

 func getLatestMessageFromFirebase(token:String,completionmessage: @escaping (_ message:String) -> Swift.Void)
    {
        print("getModelFromFirebase")
        var message:String=""
        ref.child("chatmessage/devicetoken/").child(token).queryLimited(toLast: 1).observeSingleEvent(of: .value, with: { (snapshot) in
            // Get user value
            let value = snapshot.value as? NSDictionary
            if value?["message"] as? String != ""
            {
                DispatchQueue.main.async
                    {
                        message = (value?["message"] as? String)! //My app stop on this line
                        completionmessage(message)
                }


            }
        })
        { (error) in
            print(error.localizedDescription)
        }

    }

func callAPI()
{
                                let response = (jsonResult.object(forKey: "chatListArr") as? NSArray)!
                                if response.count > 0
                                {
                                    for i in 0..<response.count
                                    {
                                        let dict = response[i] as! NSDictionary
                                        let chatlist = ChatList(dict: dict)
                                        self.arr_list.append(chatlist)
                                    }
                                    for i in 0..<self.arr_list.count
                                    {
                                        let chatlist = self.arr_list[i]
                                        self.getLatestMessageFromFirebase(token: chatlist.token, completionmessage: { (message) in
                                            self.arr_list[i].msg = message
                                        })
                                    }

                                    self.table_view.reloadData()
                                }
}

enter image description here

Please help me. Thanks in Advance.

Chris

First of all you should clean your code up a bit, you do a couple of things which would be considered anti patterns

 func getLatestMessageFromFirebase(token:String,completionmessage: @escaping (_ message:String) -> Swift.Void)
    {
        ref.child("chatmessage/devicetoken/").child(token).queryLimited(toLast: 1).observeSingleEvent(of: .value, with: { (snapshot) in
            // Get user value
            for snap in snapshot.children.allObjects as [DataSnapshot] {
              let value = snap.value as? [String: Any] ?? [:] // A good way to unwrap optionals in a single line
              if let message = value["message"] as? String {
                DispatchQueue.main.async {
                    completionmessage(message)
                  }
              }
           }
        })
        { (error) in
            print(error.localizedDescription)
        }

    }

With the above code your app shouldnt crash. And if there is a message AND it is a string (which might have been your problem before) then your callback will fire.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to get data based on value from firebase database

From Dev

Get only latest date from database (but get all rows with that latest date)

From Dev

how to get the message in NSLocalizedDescription in iOS?

From Dev

How to authenticate user in iOS app and get data from Wordpress database?

From Dev

How to get a latest files from a different location

From Dev

"if message/text exists in database", then show the message that the user created from latest to oldest

From Dev

Django Get Latest Entry from Database

From Dev

How do I get the latest Firebase and Firepad version from Firebase cdn?

From Dev

how do i get the latest meta id from database automatically

From Dev

how to get data from firebase database

From Dev

How to get data from Real-Time Database in Firebase

From Dev

How to get a single child from firebase database

From Dev

How to get data based on value from firebase database

From Dev

How to retrieve latest entry from the firebase realtime database?

From Dev

How to get value from firebase realtime database in android?

From Dev

How to get a single child from firebase database

From Dev

How to get the latest message in each conversation of a certain user in SQL?

From Dev

How to get the last 5-10 message from a database?

From Dev

How to select latest date from database in MySQL

From Dev

"if message/text exists in database", then show the message that the user created from latest to oldest

From Dev

How can i get friends latest status from facebook in ios?

From Dev

Get latest message from an IObservable

From Dev

How to get the latest backup of a database from a folder?

From Dev

Get latest message in conversation

From Dev

How to get latest data from database in django

From Dev

How to get Data from a Firebase Database and populate them in a ListView

From Dev

Get Value from Firebase Database

From Dev

How to get the most recent data from firebase database?

From Dev

How to get data from Firebase Database in a loop?

Related Related

  1. 1

    How to get data based on value from firebase database

  2. 2

    Get only latest date from database (but get all rows with that latest date)

  3. 3

    how to get the message in NSLocalizedDescription in iOS?

  4. 4

    How to authenticate user in iOS app and get data from Wordpress database?

  5. 5

    How to get a latest files from a different location

  6. 6

    "if message/text exists in database", then show the message that the user created from latest to oldest

  7. 7

    Django Get Latest Entry from Database

  8. 8

    How do I get the latest Firebase and Firepad version from Firebase cdn?

  9. 9

    how do i get the latest meta id from database automatically

  10. 10

    how to get data from firebase database

  11. 11

    How to get data from Real-Time Database in Firebase

  12. 12

    How to get a single child from firebase database

  13. 13

    How to get data based on value from firebase database

  14. 14

    How to retrieve latest entry from the firebase realtime database?

  15. 15

    How to get value from firebase realtime database in android?

  16. 16

    How to get a single child from firebase database

  17. 17

    How to get the latest message in each conversation of a certain user in SQL?

  18. 18

    How to get the last 5-10 message from a database?

  19. 19

    How to select latest date from database in MySQL

  20. 20

    "if message/text exists in database", then show the message that the user created from latest to oldest

  21. 21

    How can i get friends latest status from facebook in ios?

  22. 22

    Get latest message from an IObservable

  23. 23

    How to get the latest backup of a database from a folder?

  24. 24

    Get latest message in conversation

  25. 25

    How to get latest data from database in django

  26. 26

    How to get Data from a Firebase Database and populate them in a ListView

  27. 27

    Get Value from Firebase Database

  28. 28

    How to get the most recent data from firebase database?

  29. 29

    How to get data from Firebase Database in a loop?

HotTag

Archive