简体   繁体   English

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

[英]IOS Swift Nested DispatchGroup for Nested Network requests handling arrays

I'm experiencing crashes and I'm not too sure how to handle the situation with nested dispatchgroup inside a dispatchgroup. 我遇到崩溃,我不太确定如何使用调度组内的嵌套调度组来处理这种情况。 I know I'm doing something wrong and getting crashes and would like some help with how to handle below situation: 我知道我在做错事并且越来越崩溃,并且想要一些如何处理以下情况的帮助:

I am using IOS Swift and Firebase and basically grabbing relevant mutual friends by first grabbing a friendList, and then grabbing the friends of each of the friends on my friendList (as those are my mutual friends), if I have not grabbed them earlier (I use a list to track ids of friends Ive already grabbed), I send another network request to fb to grab the number of mutual friends between current user and mutual friend and check if they are relevant enough to be added. 我正在使用IOS Swift和Firebase并通过首先抓取某个friendList,然后在我的friendList上抓取每个朋友的朋友(因为它们是我的共同朋友)来抓取相关的共同朋友,如果我没有早先抓过他们的话(我使用列表跟踪Ive已经抓到的朋友的ID),我向fb发送另一个网络请求,以抓取当前用户和共同朋友之间的共同朋友数量,并检查它们是否足够相关。

However I have another request after that grabs school friends from firebase and I need to make sure there arent duplicate entries because there are school friends that are also mutual friends. 但是,在此之后我又提出了另一个要求,即从火力中抢夺学校朋友,我需要确保没有重复的条目,因为有些学校朋友也是共同朋友。 I'm using Dispatch groups like so: 我正在像这样使用调度组:

// 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()
}
}

I also call mutualFriendsDispatchGroup.leave() in grabMutualFriendData(...) method. 我也用grabMutualFriendData(...)方法调用internationalFriendsDispatchGroup.leave()。

I apologize for the large amount of code, I was trying to figure out basically how to put in sync lots of network requests nest in a network request to grab mutual friends and before my grab school friends so that I dont get duplicate entries on my collectionView presenting the grabbed users. 我为大量的代码表示歉意,我试图从根本上弄清楚如何使嵌套在网络请求中的大量网络请求嵌套在网络请求中,以吸引共同的朋友,然后再抓住学校的朋友,这样我就不会在collectionView上获得重复的条目展示所吸引的用户。

Note: The counter thing in filterMutualFriends(...) is a hack I was attempting that would exit out of the outer dispatchgroup once you've iterated through the friendlist of a friend. 注意:filterMutualFriends(...)中的计数器是我尝试的一种方法,一旦您遍历了朋友的好友列表,该方法便会退出外部调度组。 The outer mutual friends dispatchGroup is the one crashing. 外面的共同朋友dispatchGroup是一个崩溃的人。

Could not figure out a proper long-term solution to fix the issue, so I had to hack around it and use a bad workaround which just removes duplicate users everytime a new user is grabbed and then reloads the collectionView. 无法找到适当的长期解决方案来解决此问题,因此我不得不解决这个问题,并使用一种错误的解决方法,该方法只能在每次抓住新用户时都删除重复的用户,然后重新加载collectionView。 However, note that this will and can cause problems in the code. 但是,请注意,这将并可能导致代码出现问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM