I want to read a document, get a field from that document, and set a variable to that field's value
I expected my variable declared outside the Firebase getDocument function to be written to. The actual results are that the variable is being written to within the Firebase getDocument function but outside the function it is empty.
Here's what I have tried:
[1]: Modifying variable within firebase function - this didn't work for me because I can't translate it well with my current Swift skills
[2]: How to set variables from a Firebase snapshot (swift) - this didn't work for me because the implementation deviates by a lot from what I have right now
//open the user Firebase database by documentID
let userDocument = userdb.collection("users").document(userDocumentId)
var userjobsData = [[String:Any]]()
userDocument.getDocument { (docums, error) in
if let docum = docums, docum.exists {
//grab all jobs data
userjobsData = docum.get("jobData") as! [[String:Any]]
//sort jobs by category in alphabetical order
userjobsData = (userjobsData as NSArray).sortedArray(using: [NSSortDescriptor(key: "category", ascending: true)]) as! [[String:AnyObject]]
}
//Here userjobsData contains data
print(userjobsData)
}
//Here userjobsData is empty
print(userjobsData)