2017-12-09 32 views
1

충돌이 발생했으며 디스패치 그룹 내에 중첩 된 디스패치 그룹을 처리하는 방법이 너무 명확하지 않습니다. 나는 잘못된 일을하고 있으며 충돌이 일어나고 있고 아래의 상황을 어떻게 처리 할 수 ​​있는지에 대한 도움을 원한다는 것을 알고 있습니다 :배열을 처리하는 중첩 네트워크 요청에 대한 IOS 스위프트 중첩 된 DispatchGroup

IOS 스위프트와 파이어베이스를 사용하고 있으며 기본적으로 friendList를 잡아서 관련된 상호 친구를 움켜 잡고 있습니다. 친구 목록에있는 각 친구의 친구 (내 친구), 이전에 그 사람을 붙잡지 않았다면 (이미 친구의 아이디를 추적 할 수있는 목록을 사용합니다.) 다른 네트워크 요청을 fb에 보내고 현재 사용자와 상호 친구 사이의 상호 친구 수 및 그들이 충분히 관련성이 있는지 확인합니다.

그러나 나는 firebased에서 그 학교 친구를 쥐고 또 다른 요청이 있습니다. 나는 또한 공통 친구 인 학교 친구가 있기 때문에 중복 된 항목이 없도록해야합니다. 그래서 같은 파견 그룹을 사용하고 있습니다 :

// 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()를 호출합니다.

나는 많은 양의 코드에 대해 사과한다. 기본적으로 네트워크 요청을 네트워크 요청에 중첩시켜 상호적인 친구들을 사로 잡기 위해 그리고 친구들과 잡담을 나누기 전에 알아 낸 것이었다. 내 컬렉션의 항목 가져온 사용자를 표시합니다.

참고 : filterMutualFriends (...)의 카운터 기능은 내가 친구의 친구 목록을 반복하면 바깥 쪽 디스패치 그룹에서 벗어나는 시도입니다. 외부 상호 친구 인 dispatchGroup은 충돌하는 친구입니다.

답변

0

문제를 해결하기위한 적절한 장기 해결책을 찾지 못해서 해킹하고 새로운 사용자가 잡을 때마다 중복 사용자를 제거한 다음 collectionView를 다시로드해야하는 잘못된 해결 방법을 사용해야했습니다. 그러나 이것이 코드에서 문제를 일으킬 수 있습니다.