我遇到了崩溃,我不太确定如何处理调度组内嵌套调度组的情况。我知道我做错了什么并且发生了崩溃,希望获得有关如何处理以下情况的帮助:
我正在使用 IOS Swift 和 Firebase 并且基本上通过首先抓取一个 friendList 来抓取相关的共同 friend ,然后抓取我的 friendList 上每个 friend 的 friend (因为那些是我的共同 friend ),如果我没有捕获他们早些时候(我使用列表来跟踪我已经抓取的 friend 的 ID),我向 fb 发送另一个网络请求以获取当前用户和共同 friend 之间的共同 friend 的数量,并检查它们是否足够相关以添加。
但是,在从 firebase 抓取学校 friend 之后我还有另一个请求,我需要确保没有重复的条目,因为有些学校 friend 也是共同的 friend 。我正在像这样使用 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()。
对于大量的代码,我深表歉意,我试图找出基本上如何同步大量的网络请求嵌套在网络请求中以获取共同的 friend 以及在我获取学校 friend 之前,这样我就不会得到重复的条目在我的 collectionView 上呈现抓取的用户。
注意:filterMutualFriends(...) 中的计数器是我尝试的一个 hack,一旦您遍历 friend 的好友列表,它就会退出外部 dispatchgroup。外部的共同 friend dispatchGroup 崩溃了。
最佳答案
无法找到解决该问题的合适的长期解决方案,所以我不得不绕过它并使用一个糟糕的解决方法,它只会在每次抓取新用户时删除重复用户,然后重新加载 collectionView。但是,请注意,这将并且可能会导致代码出现问题。
关于IOS Swift Nested DispatchGroup for Nested Network 请求处理数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47731762/