I am working on a chat app and want when the user is in the bottom of the tableview to have the firebase realtime listener enabled so new rows are added and the tableview scrolls automatically, but when the user is not at the bottom of the table view I don't want the snapshot listener because it automatically scrolls the tableview while the user is trying to read the messages above.
My Solution:
var listener: ListenerRegistration!
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
heightDictionary[indexPath] = cell.frame.size.height
// Trigger pagination when scrolled to last cell
if (indexPath.row == commentArray.count - 1) {
// Listener is removed so no more realtime updates and tableview does not scroll automaticly
listener.remove()
paginate()
listernerNeedEnable = true
}
// User is at bottom of table view need to enable listener
if (indexPath.row == 0) {
print("indexPath.row == 0")
if listernerNeedEnable == true {
print("Re enable listiner")
// NEED TO RE ENABLE LISTENER HERE !!!
listernerNeedEnable = false
}
}
}