I'm trying to obtain a document from my Firestore Database and filter the returned data by what the user selects. If the user selects ["snapchat", "facebook"]
then I'm trying to only get these specific values from the database. It should return ["snapchat": username, "instagram": username, "UID": user UID]
. However, when I print out the return value of the function, it only contains the key-value is for "UID". Here is my code:
func getMediaInfo(selected: [String]) -> Dictionary<String, String> {
var mediaDict: [String: String] = [:]
let db = Firestore.firestore()
let docRef = db.collection("users").document(Auth.auth().currentUser!.uid)
docRef.getDocument { (document, error) in
if let document = document, document.exists {
let dataDescription = document.data()
for media in selected {
print(media, ":", dataDescription![media]!)
mediaDict[media] = (dataDescription![media]! as! String)
}
} else {
print("Document does not exist")
self.dismiss(animated: true, completion: nil)
}
}
mediaDict["UID"] = Auth.auth().currentUser?.uid
print("Dictionary: ", mediaDict)
return mediaDict
}
My output is :
Dictionary: ["UID": "ub8bUMYBbeSPXd8UOdJWKCnbQzJ2"]
Twitter : nrjfjfj
Phone Number : nrnrjrn
Snapchat : hello
As you can see the values are retrieved and outputted properly. However, the mediaDict on return only has the key value pair for "UID". It also seems like the function is returning before the entire dictionary is being filled up. I'm not sure why this would happen and I'm not sure how I can fix this.