2017-10-28 1 views
2

두 노드에 걸쳐 있고 'Player'노드에서 여러 번 읽어야하는 일치 정보로 컬렉션 뷰 셀을 채우려는 문제를 해결하려고합니다. 당신이 '일치'노드가 나는 player2 정보 대 재생기를 표시하기 위해 찾고 있어요 컬렉션보기 때문에 '선수'정보를 보유하고 볼 수 있듯이스위프트 Firebase가 여러 노드에서 읽음

은 여기 내 중포 기지 데이터베이스 구조

{ 
    Players: 
     LpWgezRkC6EWS0sjXEWxhFl2: { 
      userName: 'John Doe' 
      teamId: '234' 
      teamName: 'Revenge' 
      teamLogo: 'star.png' 
      etc... 
     }, 
     RfskjEWSkdsjkjdskjsd12fg: { 
      userName: 'Jane Doe' 
      teamId: '987' 
      teamName: 'Frills' 
      teamLogo: 'jag.png' 
      etc... 
     } 
    }, 
    Matches: 
     12345: { 
      User1: 'LpWgezRkC6EWS0sjXEWxhFl2' 
      User2: 'RfskjEWSkdsjkjdskjsd12fg'    
      date: '11/10/17' 
      WeekId: 19 
      etc... 
     } 
    } 
} 

입니다.

self.ref.queryOrdered(byChild: "WeekId").queryEqual(toValue: 19).observe(.value, with: { snapshot in 

    var items: [Match] = [] 

    for item in snapshot.children { 

     let snapshotValue = (item as! DataSnapshot).value as? NSDictionary 

     let pId1 = snapshotValue!["User1"] as! NSString 
     let pId2 = snapshotValue!["User2"] as! NSString 

     let match = Match(snapshot: item as! DataSnapshot) 

     items.append(match) 

    } 

    self.matches = items 

    self.collectionView?.reloadData() 
} 

나는 (I 2 필요)이 '선수'노드에 두 번째 조회 작업을 수행하는 방법에 정말 확실하지 않다가 모두를 조회해야하므로 :

내가 지금까지 가지고있는 코드는 이것이다 선수 정보, 모두 경주없이 let match = Match(snapshot: item as! DataSnapshot) 기능을 지나면, 그렇지 않으면 실패할까요?

아무도 도와 줄 수 있습니까?

답변

1

당신은 내가이 문제를 해결하려면이 올바른 방법을 생각하지 않는다 완료

func fetchUserProfile(withUID uid: String, completion: @escaping (_ profileDict: [String: Any]) -> Void) { 
    // New code 
    Database.database().reference().child(uid).observe(.value, with: { snapshot in 
     // Here you can get the snapshot of user1 
     guard let snapDict = snapshot.value as? [String: Any] else {return} 
     completion(snapDict) 
    }) 
} 

self.ref.queryOrdered(byChild: "WeekId").queryEqual(toValue: 19).observe(.value, with: { snapshot in 

     var items: [Match] = [] 

     for item in snapshot.children { 

      let snapshotValue = (item as! DataSnapshot).value as? NSDictionary 

      let pId1 = snapshotValue!["User1"] as! NSString 
      let pId2 = snapshotValue!["User2"] as! NSString 

      fetchUserProfile(withUID: pId1, completion: { (userDict1) in 
       // Here you get the userDict 1 
       self.fetchUserProfile(withUID: pId2, completion: { (userDict2) in 
        //Here you get the user dict 2 
        let match = Match(snapshot: item as! DataSnapshot) 
        items.append(match) 
       }) 
      }) 
     } 

     self.matches = items 

     self.collectionView?.reloadData() 
    }) 

// 가져 오기 사용자 프로파일을 추가 할 수 있습니다. 모든 사용자의 pID를 캡처하여 UserProfiles 배열에 저장하는 것이 좋습니다. 필요한 경우 해당 배열에서 사용자 프로파일을 가져올 수 있습니다. 희망이 도움이됩니다.

+0

Hello Rozario, 나는 이것을했다. 문제는 'Match'객체를 생성하고 UICollectionView에 추가하기 때문에 2 번째와 3 번째 룩업을 기다리지 않아야한다는 것이다. – Learn2Code

+0

완료 블록이있는 함수에 추가 할 수있다. firebase 데이터베이스가 비동기 페칭을 완료하기를 기다리는 것. 1 단계 : 완료와 함께 사용자 프로필 1을 가져옵니다. 2 단계 : profile1 완료 내에 사용자 프로필 2를 가져옵니다. 3 단계 : profile2 완료 내에서 모델 개체를 만들고 항목에 추가하십시오. –

+0

당신은 완료 핸들러를 포함한 코드로 답을 수정할 수 있습니까? – Learn2Code