0

So I have a feed that users will post into and would like each new submission to go to the top of my collection view. I am ordering each new post by descending time using the negative value of the UTC time shown here: How do you properly order data from Firebase chronologically.

    "Posts" : {
      "-KHLOy5mOSq0SeB7GBXv" : {
           "timestamp" : "1476028495915",
           "descendStamp": "-1476028495915"
           "otherData": ""
       },
      "-KHLrapS0wbjqPP5ZSUY" : {
           "timestamp" : "1476028496102"
           "descendStamp" : "-1476028496102"
           "otherData": ""
       },

However, each new real time post added is being put on the bottom when initially loaded. Believe this is due to the fact that the observer is asynchronously loading only newly added children and not downloading any other information again, which leads it to ignore my

    .queryOrderedByChild("descendStamp")

After leaving and returning to the view, it properly organizes the recently added post to the top. Do I need to client side filter in order to get these new posts at the top in real time? Or what would be the best way to reorganize the order of the posts as the newer ones come in?

    var posts = [PostModel]()

    let postQuery = ref.child("Posts").queryOrderedByChild("descendStamp")
        postQuery.observeEventType(.ChildAdded, withBlock: { (snap) in


            if snap.exists() {
                let postData = snap.value! as! [String: AnyObject]

                let post = PostModel()

                post.timestamp = postData["timestamp"] as? NSTimeInterval
                post.descendStamp = postData["descentStamp"] as? NSTimeInterval
                post.otherData = postData["otherData"] as? String

                self.posts.append(post)

                dispatch_async(dispatch_get_main_queue(), {
                    self.collectionView!.reloadData()
                })
            }
Community
  • 1
  • 1
Josh
  • 7
  • 4

1 Answers1

0

Just sort your dataSource before you update your tableView.

self.posts.sort(by: {$0.descendStamp > $1.descendStamp})

Your code would look something like this:-

var posts = [PostModel]()

let postQuery = ref.child("Posts").queryOrderedByChild("descendStamp")
    postQuery.observeEventType(.ChildAdded, withBlock: { (snap) in


        if snap.exists() {
            let postData = snap.value! as! [String: AnyObject]

            let post = PostModel()

            post.timestamp = postData["timestamp"] as? NSTimeInterval
            post.descendStamp = postData["descentStamp"] as? NSTimeInterval
            post.otherData = postData["otherData"] as? String

            self.posts.append(post)
            self.posts.sort(by: {$0.descendStamp > $1.descendStamp})

            dispatch_async(dispatch_get_main_queue(), {
                self.posts.sort(by: {$0.descendStamp > $1.descendStamp})
                self.collectionView!.reloadData()
            })
        }
Dravidian
  • 9,945
  • 3
  • 34
  • 74
  • Can't I not sort like that because it is a dictionary? Also is that syntax for swift 3..I am currently using swift 2.3 – Josh Oct 09 '16 at 19:58
  • What is `PostModel`? Isn't it a struct? – Dravidian Oct 09 '16 at 20:01
  • No, it is a class for my model post cell. Does that make a difference? – Josh Oct 09 '16 at 23:45
  • Does this work or not?And if not convert your PostModel class to a struct. – Dravidian Oct 10 '16 at 04:56
  • I changed this to a struct but still not sure how that changes posts from being a dictionary to allow the sorting you mentioned in your answer – Josh Oct 12 '16 at 12:57
  • Was able to get it work with slightly different syntax, but when I update for swift 3 i will use your code. Thanks! – Josh Oct 13 '16 at 03:20