2017-04-21 9 views
0

特定の辞書のcountでコレクションビューのnumberOfItemsInSectionメソッドを設定しようとしています。ディクショナリはFirebaseコール(その部分が重要な場合は下のコード)から設定されます。とにかくFirebaseの呼び出しは非同期で、ディスパッチキュー、クロージャー、または別の完了ハンドラと組み合わせる必要はありませんでした。Firebase呼び出しが完了した後でコレクションビューメソッドを設定する

私はreturn avatarDictionary.countnumberOfItemsInSectionを設定しようとすると、しかし、それが空だし、実際に問題の辞書0ショーを数える印刷が(印刷がいることを確認した)その値を設定しますんが、それはすべてをループする必要がありますユーザーはすべての値を取得する前にデータを取得しています。私はnumberOfItemsInSectionreturnをチェックすると、その辞書は依然として0であると考えています。

それはどういうことですか?もしそうなら、コレクションビューを設定する前にディクショナリがすべての値で完全に設定されていることを確認する最良の方法は何ですか?

コード:

func getParticipantInfo() { 

    let databaseRef = FIRDatabase.database().reference() 

    // Query Firebase users with those UIDs & grab their gender, profilePicture, and name 
    databaseRef.child("groups").child(currentRoomID).child("participants").observeSingleEvent(of: .value, with: { (snapshot) in 

     if let snapDict = snapshot.value as? [String:AnyObject] { 

      for each in snapDict { 

       let uid = each.key 
       let avatar = each.value["profilePicture"] as! String 
       let gender = each.value["gender"] as! String 
       let handle = each.value["handle"] as! String 
       let name = each.value["name"] as! String 

       // Set those to the dictionaries [UID : value] 
       self.avatarDictionary.setValue(avatar, forKey: uid) 
       self.nameDictionary.setValue(name, forKey: uid) 
       self.genderDictionary.setValue(gender, forKey: uid) 
       self.handleDictionary.setValue(handle, forKey: uid) 
       print("\n\navatarDictionary:\n \(self.avatarDictionary)") 
       print("\nhandleDictionary:\n \(self.handleDictionary)") 
       print("\ngenderDictionary:\n \(self.genderDictionary)") 
       print("\nnameDictionary:\n \(self.nameDictionary)") 
      } 
     } 
    }, withCancel: {(Err) in 
     print(Err.localizedDescription) 
    }) 
} 

答えて

1

使用これどういうわけか、私は常にリロードを忘れて(右forループの後に)配列

collectionView.reloadData() 

+0

を設定した後に。ありがとう! – KingTim

関連する問題