用于嵌套网络请求处理数组的IOS Swift嵌套DispatchGroup

时间:2017-12-09 18:26:34

标签: ios swift facebook firebase grand-central-dispatch

我遇到崩溃,我不太确定如何处理调度组内嵌套调度组的情况。我知道我做错了什么并且遇到了崩溃,并希望得到一些如何处理以下情况的帮助:

我正在使用IOS Swift和Firebase,基本上抓住一个朋友列表抓住相关的共同朋友,然后抓住我朋友列表上的每个朋友的朋友(因为那些是我的共同朋友),如果我还没抓住他们早些时候(我使用一个列表来跟踪我已经抓过的朋友的ids),我向fb发送另一个网络请求以获取当前用户和共同朋友之间的共同朋友的数量,并检查它们是否足够相关以便添加。

然而,我有另外一个请求,从firebase抓住学校的朋友,我需要确保没有重复的条目,因为有学校的朋友也是共同的朋友。我正在使用像这样的Dispatch组:

// Iterates through friendList to grab mutual friends
        for user in currUser.friendList {
            // Grabs user friend list
            let userFriendListRef = Database.database().reference().child("friend-list").child(user.userID)
            userFriendListRef.observeSingleEvent(of: .value, with: { (snapshot) in
                guard snapshot.exists(),
                    let userFriendList = snapshot.value as? [String: Any] else {
                        logger.info("No mutual friends grabbed from friend")
                        return
                }

                // Mutual friends dispatchGroup
                self.mutualFriendsDispatchGroup.enter()

                // If exists friends, then see if matches user's interest
                self.filterMutualFriendsToMatchUserInterest(using: userFriendList)
            })
        }

        self.mutualFriendsDispatchGroup.notify(queue: .main) {
            logger.info("Done mutual friends")
        }

// Checks if mutual friend matches interest and then adds it into collectionView
fileprivate func filterMutualFriends(using userFriendList: [String: Any]) {
// Maintains a counter
var searchedMutualFriendCounter = 0

// Iterates through userFriendList
for (userID, _) in userFriendList {
    searchedMutualFriendCounter += 1 // Increments counter

    // Ensures not repeating a mutual friend
    guard usersAddedToHomeScroll[userID] == nil,
        searchedUsers[userID] == nil,
        !blockedUsers.contains(userID) else {

            // Handles mutual friend dispatch group leave condition
            if searchedMutualFriendCounter == userFriendList.count {
                self.mutualFriendsDispatchGroup.leave()
                return
            }

            continue
    }

    searchedUsers[userID] = true
    grabFriendsDispatchGroup.enter()

    // Checks if has enough mutual friends, if yes, grab mutual friend data, else skip
    checkIfFriendHasEnoughMutualFriends(userID) { (result) -> Void in
        // Makes sure that has enough mutual friends
        guard result else {
            logger.info("Not enough mutual friends to show in userFriendScroll for \(userID)")
            self.grabFriendsDispatchGroup.leave()

            // Handles mutual friend dispatch group leave condition
            if searchedMutualFriendCounter == userFriendList.count {
                self.mutualFriendsDispatchGroup.leave()
            }

            return
        }
        logger.info("Mutual friend ID grabbed for \(userID)")
        self.grabMutualFriendData(userID, index: searchedMutualFriendCounter, total: userFriendList.count)
    }

}
}

  fileprivate func getAllFriends() {

    // Grabs mutual friends
    getMutualFriends()

    // Gets school friends
    getSchoolFriends()

// Reloads data after grabbing it all
grabFriendsDispatchGroup.notify(queue: .main) {
    self.collectionView.reloadData()
}
}

我还在grabMutualFriendData(...)方法中调用了mutualFriendsDispatchGroup.leave()。

我为大量的代码道歉,我试图弄清楚如何在网络请求中同步大量的网络请求,以便抓住共同的朋友,在我抓住学校的朋友之前,以便我不会得到重复的条目在我的collectionView上呈现抓取的用户。

注意:filterMutualFriends(...)中的计数器是我试图在你遍历好友的朋友列表后退出外部调度组的黑客。外部共同的朋友dispatchGroup是一个崩溃。

1 个答案:

答案 0 :(得分:0)

无法找到解决问题的正确长期解决方案,所以我不得不解决它并使用一个糟糕的解决方法,每次抓取新用户时都会删除重复的用户,然后重新加载collectionView。但请注意,这会导致代码出现问题。