Having trouble with the concurrency of this, ultimately I'm trying to add async fetched objects to an array, in the order in which the request was made, i.e. first fetch should add to array[0], second fetch should add to array[1], etc. The problem is, by the time the fetch completes, my index count has already changed, how can I pass the index count to the async block, so it uses the right one?

class BATableViewController: UITableViewController {
    var paymentArray = [[AnyObject]]()
    var keepCount = 0


func buildPaymentsArray(expenses: Array<AnyObject>!){
    for (index, expense) in enumerate(expenses) {
        // query for all Payments for that expense
        var paymentQuery = PFQuery(className: "Payment")
        paymentQuery.whereKey("expense", equalTo: expense)
        paymentQuery.findObjectsInBackgroundWithBlock({ (objects: [AnyObject]!, error: NSError!) -> Void in
            self.paymentArray[self.keepCount] = objects
            self.keepCount++
        })
    }
}

Related posts

Recent Viewed