我有一个组织文档,里面有一个Members集合,然后有一个Member文档。在成员文档内部包括作为成员的用户的地图。密钥是UserID和3个值(firstName,lastName,username)。我正在尝试将此数据加载到我的“ Event”类中,该类包含一个属于dictionarys的membersInvited属性。 Event类内部有一个获取此数据的方法,称为getOrgMembers()。即使我在firebase中有这些数据,我的字典也得到了一个nil值。我也正在使用Dispatch,但是有点新。
下面是事件类中的代码:
var membersInvited: [Member: Bool]?
func getOrgMembers(dispatch: DispatchGroup?) {
let membRef = BandzDatabase.collection("Organizations").document(currentUser.currentOrgID!).collection("Members").document("members")
membRef.getDocument { (snapshot, error) in
if let error = error {
print (error.localizedDescription)
} else {
if let data = snapshot?.data() {
for (key,value) in data {
if let membArray = value as? [String: Any] {
let username = membArray["username"] as? String
let firstName = membArray["firstName"] as? String
let lastName = membArray["lastName"] as? String
let userID = key
let member = Member(username: username ?? "", firstName: firstName ?? "", lastName: lastName ?? "", userID: userID)
self.membersInvited?.updateValue(true, forKey: member)
}
}
}
}
dispatch?.leave()
}
}
struct Member: Hashable {
var username: String
var firstName: String
var lastName: String
var userID: String
init (username: String, firstName: String, lastName: String, userID: String) {
self.username = username
self.firstName = firstName
self.lastName = lastName
self.userID = userID
}
}
下面是我从另一个类中调用此方法的地方:
func getMembers() {
showActivityIndicatory(uiView: self.view)
self.dispatchGroup.enter()
eventMade?.getOrgMembers(dispatch: self.dispatchGroup)
self.dispatchGroup.notify(queue: .main) {
//self.tableView.reloadData()
stopActivityIndicator()
print("happens")
print(self.eventMade?.membersInvited)
}
}
答案 0 :(得分:1)
经过一番研究,我发现由于我从未初始化过字典,所以每当我调用追加键值对时,由于它是可选的,它甚至都不会运行。因此,我将脱节更改为:
var membersInvited: [Member: Bool] = [:]