我想从Firebase删除帖子并刷新CollectionView

时间:2018-08-20 21:03:24

标签: ios swift firebase firebase-realtime-database

我正在Swift(iOS)中创建一个社交媒体应用,并且试图让用户删除照片,然后拉动刷新。该照片会从Firebase中删除,但是当我拉动刷新时,它不会刷新数据。我正在尝试使该应用程序像Facebook。该代码用于用户的个人资料。

这是我的代码:

override func viewDidLoad() {
    super.viewDidLoad()

    var userr = User()
    fetchPosts()
    refresher.addTarget(self, action:
        #selector(ProfileViewController.refreshStream(sender:)),
                        for: UIControl.Event.valueChanged)
    collectionview.addSubview(refresher)
    self.collectionview.reloadData()
}

@objc func refreshStream(sender: UIRefreshControl) {
    refetchPosts()
    print("refresh")
    collectionview.reloadData()

    refresher.endRefreshing()
}

func fetchPosts(){
    let ref = FIRDatabase.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 == FIRAuth.auth()?.currentUser?.uid {
                    self.following.append(FIRAuth.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 = Profile()
                                        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, let date = post["date"] as? String {

                                            posst.author = author
                                            posst.likes = likes
                                            posst.pathToImage = pathToImage
                                            posst.postID = postID
                                            posst.userID = userID
                                            posst.date = date


                                             self.posts = self.posts.sorted { $0.date > $1.date}
                                            self.posts.append(posst)
                                        }
                                    }
                                }
                                self.collectionview.reloadData()
                            }
                        }
                    })
                }
            }
        }
    })
    ref.removeAllObservers()
}

0 个答案:

没有答案