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()
})
}