1

I am working in a project that involves fetching user uploaded post. I retrieve the user post in a dictionary and add them to an array. When the user post something, I have the creation date of the post. Now the problem comes when I load these post in a collection view where the post shuffles every time I run the code.

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)
        }
    }
2
  • just sort it self.posts.sort{$0.creationDate < $1.creationDate} before reloading the data Commented Jun 29, 2019 at 21:20
  • 1
    You are the best thanks man. It worked. Commented Jun 29, 2019 at 21:26

1 Answer 1

1
self.posts.sort{$0.creationDate < $1.creationDate}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.