[英]IOS Swift Nested DispatchGroup for Nested Network requests handling arrays
我遇到崩潰,我不太確定如何使用調度組內的嵌套調度組來處理這種情況。 我知道我在做錯事並且越來越崩潰,並且想要一些如何處理以下情況的幫助:
我正在使用IOS Swift和Firebase並通過首先抓取某個friendList,然后在我的friendList上抓取每個朋友的朋友(因為它們是我的共同朋友)來抓取相關的共同朋友,如果我沒有早先抓過他們的話(我使用列表跟蹤Ive已經抓到的朋友的ID),我向fb發送另一個網絡請求,以抓取當前用戶和共同朋友之間的共同朋友數量,並檢查它們是否足夠相關。
但是,在此之后我又提出了另一個要求,即從火力中搶奪學校朋友,我需要確保沒有重復的條目,因為有些學校朋友也是共同朋友。 我正在像這樣使用調度組:
// 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(...)方法調用internationalFriendsDispatchGroup.leave()。
我為大量的代碼表示歉意,我試圖從根本上弄清楚如何使嵌套在網絡請求中的大量網絡請求嵌套在網絡請求中,以吸引共同的朋友,然后再抓住學校的朋友,這樣我就不會在collectionView上獲得重復的條目展示所吸引的用戶。
注意:filterMutualFriends(...)中的計數器是我嘗試的一種方法,一旦您遍歷了朋友的好友列表,該方法便會退出外部調度組。 外面的共同朋友dispatchGroup是一個崩潰的人。
無法找到適當的長期解決方案來解決此問題,因此我不得不解決這個問題,並使用一種錯誤的解決方法,該方法只能在每次抓住新用戶時都刪除重復的用戶,然后重新加載collectionView。 但是,請注意,這將並可能導致代碼出現問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.