2017-04-21 30 views
0

我正在尝试使用某个字典的count来设置我的收集视图的numberOfItemsInSection方法。字典是从Firebase调用中设置的(如果该部分很重要,请在下面的代码中进行设置)。我感觉Firebase调用是异步的,并且不需要与调度队列,闭包或单独的完成处理程序结合使用。只有在Firebase调用完成后才设置收集视图方法

但是,当我尝试将numberOfItemsInSection设置为return avatarDictionary.count时,它是空的,并且确实打印该计数显示为0.所讨论的字典确实使用其值设置(打印确认),但它需要遍历所有用户在获取所有数据之前先获取数据。我想当numberOfItemsInSection检查其return时,字典仍然在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

使用此设置阵列

collectionView.reloadData() 

(后为环右)

+0

不知怎的,我总是忘了重装后。谢谢! – KingTim

相关问题