How Should I Retrieve Data From Firebase And Put It Into A Dictionary?

- 1 answer

I have an organization document with a Members collection inside of it and then a members document inside of that. Inside the members document includes a Map of a user which is a member. The Key is the UserID and 3 values (firstName, lastName, username). I am trying to load in this data into my "Event" class that holds a membersInvited Property that is a dictionary. Inside the Event class is a method to get this data called getOrgMembers(). Even though I have that data in firebase I am getting a nil value for my dictionary. I also am using Dispatch but kind of new to it.

Below is code in the Event Class:

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)


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


Below is were I call this method from another class:

func getMembers() {
    showActivityIndicatory(uiView: self.view)
    eventMade?.getOrgMembers(dispatch: self.dispatchGroup)
    self.dispatchGroup.notify(queue: .main) {


After some research, I discovered that since I never initilized the dictionary, whenever I was calling to append key-value paires it would not even run since it was an optional. So I changed the decleration to this:

var membersInvited: [Member: Bool] = [:]