将dispatchGroup正确放置到reloadData

时间:2018-02-14 19:10:11

标签: ios swift uitableview grand-central-dispatch

我有一个tableview函数,它从数据库中提取数据以呈现单元格。我想完成不重新加载我的tableview的目标。我了解到派遣小组将是一种方式,因为我不想回到重新加载tableView的完成块,直到所有数据都被拉出,但是当我使用dispatchGroup时它永远不会达到完成它只是停止。我的变量的位置可能在错误的位置,但我真的不知道我应该把它放在哪里。我一直把它搬到不同的地方,但仍然没有。

import UIKit
import Firebase

class FriendsEventsView: UITableViewController{
    var cellID = "cellID"
    var friends = [Friend]()
    var attendingEvents = [Event]()
    //label that will be displayed if there are no events
    var currentUserName: String?
    var currentUserPic: String?
    var currentEventKey: String?

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "Friends Events"
        view.backgroundColor = .white
        // Auto resizing the height of the cell
        tableView.estimatedRowHeight = 44.0
        tableView.rowHeight = UITableViewAutomaticDimension
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "close_black").withRenderingMode(.alwaysOriginal), style: .done, target: self, action: #selector(self.goBack))
        tableView.register(EventDetailsCell.self, forCellReuseIdentifier: cellID)
        self.tableView.tableFooterView = UIView(frame: CGRect.zero)



    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        DispatchQueue.global(qos: .background).async {
            print("This is run on the background queue")
            self.fetchEventsFromServer { (error) in
                if error != nil {
                    print(error)
                    return
                } else {
                    DispatchQueue.main.async {
                        self.tableView.reloadData()
                        print("This is run on the main queue, after the previous code in outer block")
                    }

                }
            }


        }

    }

    @objc func goBack(){
        dismiss(animated: true)
    }
    override func numberOfSections(in tableView: UITableView) -> Int {
       // print(friends.count)
        return friends.count
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       // print(friends[section].events.count)
        return friends[section].collapsed ? 0 : friends[section].events.count
    }
    func tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellID) as! EventDetailsCell? ?? EventDetailsCell(style: .default, reuseIdentifier: cellID)
       // print(indexPath.row)
        cell.details = friends[indexPath.section].events[indexPath.row]
        return cell
    }

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "header") as? CollapsibleTableViewHeader ?? CollapsibleTableViewHeader(reuseIdentifier: "header")
       // print(section)
        header.arrowLabel.text = ">"
        header.setCollapsed(friends[section].collapsed)
        print(friends[section].collapsed)
        header.section = section
       // header.delegate = self
        header.friendDetails = friends[section]
        return header
    }
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50
    }
   func fetchEventsFromServer(_ completion: @escaping (_ error: Error?) -> Void ){
        //will grab the uid of the current user

        guard let myUserId = Auth.auth().currentUser?.uid else {
            return
        }
        let ref = Database.database().reference()
        //checking database for users that the current user is following
        ref.child("following").child(myUserId).observeSingleEvent(of: .value, with: { (followingSnapshot) in
           //handling potentail nil or error cases
            guard let following = followingSnapshot.children.allObjects as? [DataSnapshot]
                else {return}

            //validating if proper data was pulled
            let group = DispatchGroup()

            for followingId in following {
                group.enter()
                UserService.show(forUID: followingId.key, completion: { (user) in
                    PostService.showFollowingEvent(for: followingId.key, completion: { (event) in
                        self.attendingEvents = event
                        var friend = Friend(friendName: (user?.username)!, events: self.attendingEvents, imageUrl: (user?.profilePic)!)
                        self.friends.append(friend)
                    })
                })
            }

此循环应在执行此if语句后返回viewWillAppear中的completon块

            if self.friends.count == following.count{
                group.leave()
                let result = group.wait(timeout: .now() + 0.01)
                //will return this when done
                completion(nil)
            }

        }) { (err) in
            completion(err)
            print("Couldn't grab people that you are currently following: \(err)")
        }

    }

非常感谢任何帮助

1 个答案:

答案 0 :(得分:2)

您希望将group.leave()置于PostService.showFollowingEvent回调中。

现在您拨打enter following.count - 次,但只拨打leave一次。要让小组继续,您必须在输入时多次离开小组:

        for followingId in following {
            group.enter()
            UserService.show(forUID: followingId.key, completion: { (user) in
                PostService.showFollowingEvent(for: followingId.key, completion: { (event) in
                    self.attendingEvents = event
                    var friend = Friend(friendName: (user?.username)!, events: self.attendingEvents, imageUrl: (user?.profilePic)!)
                    self.friends.append(friend)
                    // leave here
                    group.leave()
                })
            })
        }

此外,我不建议使用group.wait,因为您可能遇到死锁。如果调用group.leave的任何回调发生在与group.wait相同的线程上,则它们将永远不会被调用,您将最终得到冻结的线程。相反,请使用group.notify

    group.notify(queue: DispatchQueue.main) {
        if self.friends.count == following.count {
            completion(nil)
        }
    }

这将允许在主线程上执行,但是一旦所有任务完成,它将执行提供的回调闭包。