jessesquires / JSQMessagesViewController

An elegant messages UI library for iOS

Home Page:https://www.jessesquires.com/blog/officially-deprecating-jsqmessagesviewcontroller/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Messages not returing desired output after retrieving from Firebase

chand97 opened this issue · comments

I'm trying to display all of my messages in a tableView. I'm retreiving them in my ' viewWillAppear ' method and when I print one of my messages array property i do get the proper output but thats not the case in the ' cellForRowAt indexPath ' method. The tableView shows messages but they are all same:

pic

lass messagesModel: NSObject {
    
    var senderDisplayName : String?
    var mediaType : String?
    var messageID : String?
    var randomString : String?
    var reciverID : String?
    var senderID : String?
    var senderPhotoURL : String?
    var text : String?
    var time : String?
    var lattitude : Double?
    var longitude : Double?
    var fileURL : String?

}
let message = messagesModel()
        
        self.messages.removeAll()
        
        for converstation in self.filterdTerms {
            
            let child = converstation
            
            FIRDatabase.database().reference().child("messages").child(child).queryLimited(toLast: 1).observe(.childAdded, with: { (snapshot) in
                
                if let dictionary = snapshot.value as? [String : AnyObject] {
                    
                    let newMessage = dictionary
                    
                    message.setValuesForKeys((newMessage as [String : Any]))
                    
                    self.messages.append(message)
                    print(self.messages.last?.time! as! String)
                    
                        DispatchQueue.main.async {
                            
                            self.tableView.reloadData()
                            
                        }
                }
                
            })
            
        }
   override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? UserCell
        
        let message = messages[indexPath.row]

        var recieverName : String!
        var recieverPhotoURL : String!
        
        FIRDatabase.database().reference().child("users").child(message.reciverID!).child("credentials").observeSingleEvent(of: .value, with: { (snapshot) in
            
            if let dictionary = snapshot.value as? [String : AnyObject] {
                
                recieverName = dictionary["username"] as! String
                recieverPhotoURL = dictionary["photoUrl"] as! String
                
                if FIRAuth.auth()?.currentUser?.uid != message.senderID {

                    cell?.textLabel?.text = message.senderDisplayName
                    cell?.profileImageView.loadImageUsingCacheWithUrlString(message.senderPhotoURL!)
                    
                }else if FIRAuth.auth()?.currentUser?.uid == message.senderID {
                    
                    cell?.textLabel?.text = recieverName
                    cell?.profileImageView.loadImageUsingCacheWithUrlString(recieverPhotoURL)
                }
            }
        })
        
        if message.mediaType == "Text" {
            
            cell?.detailTextLabel?.text = message.text
            
        }else if message.mediaType == "Photo" {
            
            cell?.detailTextLabel?.text = "Photo"
            
        }else if message.mediaType == "Video" {
            
            cell?.detailTextLabel?.text = "Video"
            
        }else if message.mediaType == "Location" {
            
            cell?.detailTextLabel?.text = "Location"
            
        }else if message.mediaType == "Audio" {
            
            cell?.detailTextLabel?.text = "Audio"
        }
        
        return cell!
    }

@chand97 The issue does not seem to be related to the library. Can you confirm and provide more information? Thanks.

Yup I've already solved it

@chand97 thanks for confirming! 👍