简体   繁体   中英

IOS : message doesn't appear immediately in tableview from firebase database

when i add first message it's appear immediately in the tableview but next messages don't appear i have to back and return to the same chat to get them

retrieve messages method

my database

viewDidLoad `

   func fetchMessage(key1:String){
    let mydatabase = FIRDatabase.database().reference().child("message")
    let query = mydatabase.queryOrderedByKey().queryEqual(toValue: key1)
        query.observe(.childAdded) { (snapshot) in
            for dict in snapshot.children{
                let childsnap = dict as! FIRDataSnapshot
                let dict2 = childsnap.value as! [String: Any]
                var message = chatmssage()
                let body = dict2["MesageBody"]! as? String
                let email = dict2["from"]! as? String
                message.body = body
                message.email = email
                self.chatmessagearr.append(message)
                self.mytableview.reloadData()
            }
        }
}

override func viewDidLoad() {
    super.viewDidLoad()
    mytableview.delegate = self
    mytableview.dataSource = self
    self.tabBarController?.tabBar.isHidden = true
    messageTXTF.delegate = self
    mytableview.separatorStyle = .none
    let curID = (user!["id"] as? String)!
    let toID = self.reciever.uid!
    key = (curID < toID ? curID+toID : toID+curID )
    fetchMessage(key1:key!)
}

You should call self.myTableView.reloadData() from the main thread after your fetch completes. Like this,

DispatchQueue.main.async {
    self.myTableView.reloadData()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM