0

So I'm trying to not only append a new element to my array, I also want to have that new and/or updated element move to the top of my tableView, like on most messaging apps. My data is on Firebase and I'm pulling down values and storing them in an array called feedUpdate. I've tried a few different approaches to this problem but the solution eludes me:

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

 if (senderNumber == userPhoneNumber || receiverNumber == userPhoneNumber) {                   
 let sender = senderNumber; let receiver = receiverNumber; let messagebody = message

 //This functions determines if I've sent or received messages from a user            
if firstContact(sender: sender, receiver: receiverNumber){

let newMessage = FeedMessage(sender: sender, receiver: receiver, messageBody: messagebody)

feedUpdate.insert(newMessage, at: 0)
}

I thought .insert would do the trick by setting the index destination to that of the first element's but it's not working, .reversed() was similarly disappointing...

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
KarmaDeli
  • 610
  • 1
  • 6
  • 16
  • I would need to see your database to give you an answer, but have you tried using `childByAutoID()` when creating your messages? It'll sort it automatically when fetching it to a datasource? – Torewin Oct 27 '17 at 01:43
  • I'm still in prerelease development so my database structure is simple with only 2 user for testing. I used childByAutoID() for when a message gets saved to firebase but my messages are at a location that created: iMessages/Messages. You're there does seem to be some auto sorting going on courtesy of Firebase but it's not going to put new messages at the top of my tableView automatically – KarmaDeli Oct 27 '17 at 01:52
  • Simple enough - do you have a node that you are able to sort by? Like a time stamp? – Torewin Oct 27 '17 at 01:53
  • No time stamp at the moment. Are you staying Firebase is capable of changing the order of the elements in my array? – KarmaDeli Oct 27 '17 at 02:15
  • Yes you can simply add timestamp to your messages, and then retrieve these messages from firebase in a sorted order base on this timestamp, that will give you the latest message on the top, check this link for more help: https://stackoverflow.com/questions/37144030/how-do-you-properly-order-data-from-firebase-chronologically – 3stud1ant3 Oct 27 '17 at 02:48
  • Took the words out of my mouth - let me know if you need help doing that. – Torewin Oct 27 '17 at 10:29
  • @Torewin, I think I'm on the verge of finding a solution to this by using .insert with a combination of the right booleans, there are some weird bugs caused by what I assume is my observer looping over all the childDBs. 3stud's link may come in handy if my logic continues to not get the job done right, but at the moment its unfamiliar to me and it seems too verbose for its simple utility, I mean, I'm just trying to sort my array in descending order without my observing adding duplicates, ukno. – KarmaDeli Oct 27 '17 at 11:48
  • You must have some type of logic behind it that you can use to sort it. Timestamps would be the most obvious one. If you had a timestamp in each node in Firebase you will be able to sort it pretty easily. – Torewin Oct 27 '17 at 20:29
  • @Torewin I tried the time sort method but it actually set me back a bit and now my tableView is blank. For what is worth, here's a link to my new question related to this same issue but with my attempt at implementing the timeSort: https://stackoverflow.com/questions/46991529/how-make-your-tableview-show-new-or-update-element-at-the-top-of-your-tableview?noredirect=1#comment80929745_46991529 – KarmaDeli Oct 28 '17 at 15:56

0 Answers0