Hi Victor,
This is what worked for me....basically taking the same logic from the HomeViewController that handles the likes and comments and adding it into a check of IsGridView...hope this helps....sorry for the delay!
fileprivate func fetchOrderedPosts(user: AppUser) {
if isGridView {
let ref = Database.database().reference().child("posts").child(user.uid)
ref.observeSingleEvent(of: .value, with: { (snapshot) in
self.collectionView?.refreshControl?.endRefreshing()
guard let dictionaries = snapshot.value as? [String: Any] else { return }
dictionaries.forEach({ (key, value) in
guard let dictionary = value as? [String: Any] else { return }
var post = Post(user: user, dictionary: dictionary)
post.id = key
Database.database().reference().child("likes").child(key).child(user.uid).observeSingleEvent(of: .value, with: {
(snapshot) in
if let value = snapshot.value as? Int, value == 1 {
post.hasLiked = true
} else {
post.hasLiked = false
}
self.posts.append(post)
self.posts.sort(by: { (p1, p2) -> Bool in
return p1.creationDate.compare(p2.creationDate) == .orderedDescending
})
self.collectionView?.reloadData()
}, withCancel: { (err) in
print("Failed to fetch like info for post:", err)
})
Database.database().reference().child("likes").child(key).child(user.uid).observe(.childChanged, with: { (snapshot) in
if let value = snapshot.value as? Int, value == 1 {
post.hasLiked = true
} else {
post.hasLiked = false
}
self.collectionView?.reloadData()
})
})
}) { (err) in
print("Failed to fetch posts:", err)
}
} else {
let ref = Database.database().reference().child("posts").child(user.uid)
self.collectionView?.refreshControl?.endRefreshing()
//perhaps later we will add some pagination of data
ref.queryOrdered(byChild: "creationDate").observe(.childAdded, with: { (snapshot) in
guard let dictionary = snapshot.value as? [String: Any] else { return }
//guard let user = self.user else { return }
let post = Post(user: user, dictionary: dictionary)
self.posts.insert(post, at: 0)
self.collectionView?.reloadData()
}) { (err) in
print("Failed to fetch ordered post:", err)
}
}
}