0

I can't seem to get new messages sent from new user or new message sent from users already in tableview to go to the like with most messaging apps. I've ask this question before and tried to follow the directions in a link that someone kind enough to send me (How do you properly order data from Firebase chronologically) but I still can't get the desired effect no matter matter I try

func getChatsFeed(){
//following the links instruction "timeStamp" is suppose help return fireBase children in reverse order. "atValue: time" is a value for the time when my viewDidload.
    let queryChats = Database.database().reference().queryOrdered(byChild: "timeStamp").queryEnding(atValue: time, childKey: "timeStamp")

    queryChats.observe(.childAdded) { (snapshot) in

        if let snapvalue = snapshot.value as? Dictionary<String,Any>, let
            sender = snapvalue["sender"] as? String, let receiver =
            snapvalue["receiver"] as? String, let message = snapvalue["messageBody"] as?
            String, let key = snapvalue["key"] as? String, let timeStamp = snapvalue["timeStamp"] as? String{

            let newMessage = FeedMessage(sender: sender, receiver: receiver, messageBody: message, key: key, timeStamp: timeStamp)

            feedUpdate.append(newMessage)
            self.chatsTableView.reloadData()

        }


    }
}

The link I included received a respectable amount of praise for being solution a to a lot of StackOverflow users problem so maybe I messed up somewhere, being not fully in the know of all the different Firebase methods, this could be the case. Any pointers anyone?

KarmaDeli
  • 610
  • 1
  • 6
  • 16
  • So is the new data appearing in your table at all? – Martin Muldoon Oct 28 '17 at 15:50
  • Not with this approach no. But others approaches that I've tried, though they populate my tableView, they don't move new/changed data source elements to the top of the tableView like I want it to. – KarmaDeli Oct 28 '17 at 15:54
  • When you print it out, is it sorted? – Torewin Oct 29 '17 at 14:14
  • @Torewin I actually found a solution to my issue. My logic was flawed at first but I worked on it and thought that if I had a value stored telling whether users ever had a conversation and then just updated the messagBody property values by iterating through and updating my array of feedMessage objects that had the same sender and receiver. It's still not perfect but it works without any weird bugs or surprises. My next step is to be able to send background notifications to users when they receive a new message. I already attempted to code this functionality but the nothing gets triggered – KarmaDeli Nov 01 '17 at 15:52
  • Glad you got it to work! Firebase/Google has some nice tutorials to get you started on notifications. You will need an app server to process the notifications or have your app always be running in the background. – Torewin Nov 01 '17 at 16:41
  • An app server? Is this in addition to the Firebase servers I'm currently using, namely Firebase and Firebase Messaging? – KarmaDeli Nov 01 '17 at 16:51

1 Answers1

0

With the other approaches, you've used where data is loaded into feedUpdate, simply do this:

    chatsTableView.reloadData()
    let indexPath:IndexPath = IndexPath(row:0, section:0)
    chatsTableView.scrollToRow(at: indexPath, at: UITableViewScrollPosition .top, animated: true)
Martin Muldoon
  • 3,388
  • 4
  • 24
  • 55
  • @MM I'm sorry for not being clearer about my issue, I'd like the data in the cells to change if new data alters my datasource ie if I get an initial message from a user and if I get subsequent messages from a user that already chatted with. if you can pull up the Messages app on your iphone-- I'm essential trying to mimic the behaviors of the initial TableViewController on that app. Your answer, while it appears to be spot on, is already successfully implemented on my one-on-one messagesVC which mimics the VC that loads when you tap a message in the Messages app on your iphone. – KarmaDeli Oct 28 '17 at 16:13