Swift scoping issue

101 views Asked by At

I have an app that is pulling info from a parse.com database and passing it into an array. When I println() this array from inside the while loop, it prints fine. When I try to print it outside the while loop, it returns empty. Here is my code:

var players = [String]()
var total = [String]()

var addTotal:AnyObject!
var addTotalFinal:Int!

var addPlayers:AnyObject!
var addPlayersFinal:Array<Int>!

@IBOutlet weak var tableView: UITableView!
var test: AnyObject!
override func viewDidLoad() {
    super.viewDidLoad()

     Parse.setApplicationId("KZ758LUTQZQ9Kl69mBDkv7BNLGHyXeKmqtFf7GmO", clientKey: "lOK5rg7wKeTRXZGV7MZBlQ5PTpNlGO0mkgtLkLgH")

    var query = PFQuery(className:"runningTotal")
    query.whereKey("total", notEqualTo: 0)
    query.findObjectsInBackgroundWithBlock
    {
        (objects: [AnyObject]!, error: NSError!) -> Void in
        if error == nil
        {

            var i = 0

            while i < objects.count
            {

            self.addTotal = objects[i]["total"]
            self.addTotalFinal = self.addTotal as Int

            self.addPlayers = objects[i]["players"]
            self.addPlayersFinal = self.addPlayers as Array

            self.players.append("\(self.addPlayersFinal)")
            self.total.append("\(self.addTotalFinal)")

                ++i
            }



        }
        else
        {

        }




    }

    navigationItem.title = "\(players)"



 }


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return players.count
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    var cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) as UITableViewCell
    cell.textLabel.text = "\(players[indexPath.row]) - \(total[indexPath.row])"

    return cell

}

The println() in the while statement returns

[[1, 11, 12, 24, 25]]
[2]

[[1, 11, 12, 24, 25], [1, 5, 24, 25, 31]]
[2, 4]

[[1, 11, 12, 24, 25], [1, 5, 24, 25, 31], [2, 12, 25, 15, 31]]
[2, 4, -1]

[[1, 11, 12, 24, 25], [1, 5, 24, 25, 31], [2, 12, 25, 15, 31], [24, 22, 25, 31, 20]]
[2, 4, -1, -6]

[[1, 11, 12, 24, 25], [1, 5, 24, 25, 31], [2, 12, 25, 15, 31], [24, 22, 25, 31, 20], [1, 2, 3, 4, 5]]
[2, 4, -1, -6, 5]

[[1, 11, 12, 24, 25], [1, 5, 24, 25, 31], [2, 12, 25, 15, 31], [24, 22, 25, 31, 20], [1, 2, 3, 4, 5], [1, 10, 11, 2, 12]]
[2, 4, -1, -6, 5, 1]

This makes sense, as there are six iterations. However, if I print inside the viewDidLoad statement instead of inside the while statement, it returns [][]

Any help is greatly appreciated.

1

There are 1 answers

12
matt On BEST ANSWER

The problem is that you have not understood how threads / asynchronous execution works. Let's be simpler. This is pseudo-code:

func viewDidLoad {
    doStepOne() // you configure your query here
    doStepTwoWithAsynchBlock {
        doStepTwo() // your while loop is here
    }
    doStepThree() // your failing println is here
}

This executes in the order doStepOne(), doStepThree(), doStepTwo(). So if doStepThree() tries to get a value that doStepTwo() sets, it isn't ready yet.

The asynch block is something that will happen, some time in the future - after all your other code has finished, including the whole of the rest of viewDidLoad.