1

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.

4
  • You have a race condition. The code in the closure is happening on a separate thread. By the time you print your array the closure code has not completed. Commented Mar 27, 2016 at 2:36
  • This has been dealt with many, many times on Stack Overflow, so please search before asking. See, for example, my answer here: stackoverflow.com/a/33699235/341994 Commented Mar 27, 2016 at 2:37
  • @matt Yes, i learned about async functions, I obviously did not learn them, which is why I am asking here Commented Mar 27, 2016 at 2:53
  • @ryantxr Thanks. I thought that having it completed in the 'getItemList' function would have actually been fully completed Commented Mar 27, 2016 at 3:13

1 Answer 1

1

The reason is that the line where you do print(taskListItems) is called before print(result) in your block. This is because the HTTP request is asynchronous, and executes the code inside the block after it's finished (which is after print(taskListItems) is called). What you should do is have your code where you use the results inside the block.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I am using a tableview to display all of the entries so I think I need to have it accessable from multiple places. What I did was just update my tableview and then appeared.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.