我的应用程序出现问题,我正在开发类似Twitter / Instagram的应用程序,该应用程序发布了图片,然后在“主页”部分中将其检索。但是,当在Firebase中添加新帖子时,我无法找到一种使收藏夹视图更新的方法。我尝试了self.collectionview.reloadData(),但是它没有用,我尝试了self.posts.removeAll(),然后是self.fetchPosts()再次检索了它,但它不起作用。我已经在这个问题上工作了几个月了,只是找不到一个“简短的问题”而不必为美元支付费用的解决方案。 预先感谢!
这是我的代码:
@IBOutlet weak var collectionview: UICollectionView!
var posts = [Post]()
var following = [String]()
lazy var refreshControl: UIRefreshControl? = {
let refreshControl = UIRefreshControl()
refreshControl.tintColor = UIColor.gray
refreshControl.addTarget(self, action: #selector(refreshList), for: UIControlEvents.valueChanged)
collectionview.addSubview(refreshControl)
self.collectionview.reloadData()
return refreshControl
}()
override func viewDidLoad() {
super.viewDidLoad()
setupNavigationBarItems()
fetchPosts()
collectionview.refreshControl = refreshControl
}
func setupNavigationBarItems() {
navigationItem.title = "Home"
}
@objc func refreshList() {
let deadline = DispatchTime.now() + .milliseconds(1000)
DispatchQueue.main.asyncAfter(deadline: deadline) {
self.collectionview.reloadData()
self.refreshControl?.endRefreshing()
print("reloaded...")
}
self.collectionview.reloadData()
}
@objc func fetchPosts() {
let ref = Database.database().reference()
ref.child("users").queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in
let users = snapshot.value as! [String : AnyObject]
for (_,value) in users {
if let uid = value["uid"] as? String {
if uid == Auth.auth().currentUser?.uid {
if let followingUsers = value["following"] as? [String : String]{
for (_,user) in followingUsers{
self.following.append(user)
}
}
self.following.append(Auth.auth().currentUser!.uid)
ref.child("posts").queryOrderedByKey().observeSingleEvent(of: .value, with: { (snap) in
let postsSnap = snap.value as! [String : AnyObject]
for (_,post) in postsSnap {
if let userID = post["userID"] as? String {
for each in self.following {
if each == userID {
let posst = Post()
if let author = post["author"] as? String, let likes = post["likes"] as? Int, let pathToImage = post["pathToImage"] as? String, let postID = post["postID"] as? String {
posst.author = author
posst.likes = likes
posst.pathToImage = pathToImage
posst.postID = postID
posst.userID = userID
if let people = post["peopleWhoLike"] as? [String : AnyObject] {
for (_,person) in people {
posst.peopleWhoLike.append(person as! String)
}
}
self.posts.append(posst)
}
}
}
self.collectionview.reloadData()
}
}
})
}
}
}
})
ref.removeAllObservers()
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.posts.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "postCell", for: indexPath) as! PostCell
cell.postImage.downloadImage(from: self.posts[indexPath.row].pathToImage)
cell.authorLabel.text = self.posts[indexPath.row].author
cell.likeLabel.text = "\(self.posts[indexPath.row].likes!) Likes"
cell.postID = self.posts[indexPath.row].postID
for person in self.posts[indexPath.row].peopleWhoLike {
if person == Auth.auth().currentUser!.uid {
cell.likeBtn.isHidden = true
cell.dislikeBtn.isHidden = false
break
}
}
return cell
}
}
答案 0 :(得分:1)
替换此
ref.child("posts").queryOrderedByKey().observeSingleEvent(of: .value, with: { (snap) in
使用
ref.child("posts").observe(of: .childAdded, with: { (snap) in
能够收听所有添加的帖子