完成问题

时间:2018-04-26 03:22:15

标签: ios swift grand-central-dispatch

所以我正在尝试为我的初始视图控制器实现一个revelaingSplashView。我的初始vc有几个数据库调用似乎以奇怪的顺序完成。在进行了一些跟踪之后,我将其缩小到最后一个特定函数,直到完成块为止。我在这里列出了

@objc func grabFriendsEvents(){
    print("Attempting to see where your friends are going")
    UserService.following { (user) in
        for following in user {
            print(following.username as Any)
            PostService.showFollowingEvent(for: following.uid, completion: { (event) in
                self.friendsEvents.append(event)
               // self.friendsEvents.append(contentsOf: event)
                // leave here
                self.friendsEvents = self.friendsEvents.removeDuplicates()
                print("ending in friends events")
                self.collectionView?.reloadData()
            })

        }  

    }
}

我已实施的服务方法中的两个支持功能如下所示。

static func following(for user: User = User.current, completion: @escaping ([User]) -> Void) {
    // 1
    let followingRef = Database.database().reference().child("following").child(user.uid)
    followingRef.observeSingleEvent(of: .value, with: { (snapshot) in
        // 2
        guard let followingDict = snapshot.value as? [String : Bool] else {
            return completion([])
        }

        // 3
        var following = [User]()
        let dispatchGroup = DispatchGroup()

        for uid in followingDict.keys {
            dispatchGroup.enter()

            show(forUID: uid) { user in
                if let user = user {
                    following.append(user)
                }

                dispatchGroup.leave()
            }
        }

        // 4
        dispatchGroup.notify(queue: .main) {
            completion(following)
        }
    })
}

第二个列在这里

static func showFollowingEvent(for followerKey: String,completion: @escaping (Event) -> Void) {
    //getting firebase root directory
    let ref = Database.database().reference()

    ref.child("users").child(followerKey).child("Attending").observeSingleEvent(of: .value, with: { (attendingSnapshot) in
        print(attendingSnapshot)
        guard var eventKeys = attendingSnapshot.children.allObjects as? [DataSnapshot] else{return}
        for event in eventKeys{
            let dispatchGroup = DispatchGroup()
            dispatchGroup.enter()
            EventService.show(forEventKey: event.key, completion: { (event) in
                dispatchGroup.leave()
                completion(event!)
            })
        }
    }) { (err) in
        print("couldn't grab event info",err)

    }
}

现在,grabFriendsEvents会返回几次,具体取决于您拥有的朋友数量和他们参加的活动。我试图弄清楚何时完成所以我可以忽略这一揭示性的观点。我认为调度组可能是要走的路,但我不确定究竟要放在哪里。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

  1. 在grabFriendsEvents中:PostService.showFollowingEvent也是一个在循环中调用的asyn函数,因此你也需要为它添加一个调度队列。

    let dispatchGroup = DispatchGroup()
    for following in user {
        print(following.username as Any)
        dispatchGroup.enter()
        PostService.showFollowingEvent(for: following.uid, completion: { (event) in
            self.friendsEvents.append(event)
           // self.friendsEvents.append(contentsOf: event)
            // leave here
            self.friendsEvents = self.friendsEvents.removeDuplicates()
            print("ending in friends events")
            self.collectionView?.reloadData()
            dispatchGroup.leave()
        })
    
    } 
    dispatchGroup.notify(queue: .main) {
        // dismiss the revealing view
    }
    
  2. 在showFollowingEvent中,你永远不会调用dispatchGroup.notify,这就是它多次触发的原因

  3. 我知道这可能是不可能的,但重组API调用最好