I just learned about Async functions in a question I posted earlier and I needed to incorporate another one. I am reading a list of entries from Firebase, return a list of them, and assigning them to an array.
Now the problem is, when I return the array to the calling function, the array is now empty and I am not sure why.
This is how I am collecting the Firebase entries (works fine):
func getListItems(uid: String, completion: (list: Array<String>?) -> Void) {
let ref = Firebase(url: getFirebaseURL() + "/" + uid)
var taskList = [String]()
ref.observeSingleEventOfType(.Value, withBlock: { snapshot in
let enumerator = snapshot.children
while let rest = enumerator.nextObject() as? FDataSnapshot {
taskList.append(rest.value as! String)
}
completion(list: taskList)
})
}
And this is how I am calling that function and trying to assign it to a local array so I can add those items to a TableView:
// Retrieve database items
let uid = def.valueForKey("uid") as! String
getListItems(uid) {
(result) in
self.taskListItems = result!
//print(result) // This prints out an array filled with my data
}
print(taskListItems) // This prints an empty array
If I do print(result) it spits out a perfect array, but not when I print the local array I am trying to use. I have defined taskListItems as: var taskListItems = [String]()
I have also tried using .append which did not work either so I'm really lost on what is going on.