<分区>
我正在从事一个涉及获取用户上传的帖子的项目。我在字典中检索用户帖子并将它们添加到数组中。当用户发布内容时,我有帖子的创建日期。现在,当我将这些帖子加载到一个 Collection View 中时,问题就来了,每次我运行代码时,帖子都会随机播放。
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)
}
}