我正在一个涉及获取用户上传帖子的项目中工作。我在字典中检索用户帖子,并将它们添加到数组中。当用户发布内容时,我具有发布的创建日期。现在,当我在集合视图中加载这些帖子时,问题就来了,每次我运行代码时,帖子都会随机播放。
fileprivate func fetchPostsWithUser(user: User) {
let ref = Database.database().reference().child("posts").child(user.uid)
ref.observeSingleEvent(of: .value, with: { (snapshot) in
guard let dictionaries = snapshot.value as? [String: Any] else { return }
dictionaries.forEach({ (key, value) in
guard let dictionary = value as? [String: Any] else { return }
let post = Post(user: user, dictionary: dictionary)
print(post.creationDate) // 2019-06-07 20:53:14 +0000
self.posts.append(post)
})
self.collectionView?.reloadData()
}) { (err) in
print("Failed to fetch posts:", err)
}
}
答案 0 :(得分:1)
self.posts.sort{$0.creationDate < $1.creationDate}