I know there are some similar questions here on SO, however I still cannot solve my issue and get to the point.
The problem is simple: I want to get items from Firebase database in reversed order. I use .reverse() to do so. It works for some meaning of the word, the items are really rearranged, but the second one, for some reason, always goes last.
Could someone please explain me the proper way to do reversing in such case? I would prefer to make reversing without creating new array. Is it possible? Here is my sample code.
Any help would be greatly appreciated, thank you!
Database.database().reference().child("posts").observe(.childAdded) { (snapshot) in
let postId = snapshot.key
guard let dictionary = snapshot.value as? Dictionary<String, AnyObject> else { return }
let post = Post(postid: postId, dictionary: dictionary)
postsArray.append(post)
postsArray.reverse()
self.finishedFetching = true
self.collectionView.reloadData()
}
With childAdded
postsArray.append(post)
postsArray.reverse()
every call of the completion appends to the array and reverses it , which will result in an array that's not the result of reversing all the array at one time , so you may observe the .value
not .childAdded