0

I have a strange issue. The following block of code is placed in my viewDidAppear section of the first View Controller, and when it runs, println(latArray), println(longArray), println(addressArray), all return no value. In the console it returns [] for all three arrays. HOWEVER, when I go to another view controller, and go back, it will populate with the data from Parse. Why is this? Why wont latArray, longArray, and addressArray populate when the app is loaded the first time with the viewWillAppear method?

    var query = PFQuery(className: "NewLog")

    // Add a where clause if there is a search criteria
    query.whereKey("Type", containsString: newRecordCreated)

    println(query)

    query.findObjectsInBackgroundWithBlock({
        (objects, error) -> Void in
        if error == nil {
            // Results were successfully found
            if let objects = objects as? [PFObject] {
                for object in objects {
                    //println(object["follower"])
                    latArray.append(object["Lat"] as! Double)
                    longArray.append(object["Long"] as! Double)
                    addressArray.append(object["Address"] as! String)
                }
            }
            // self.tableView.reloadData()
        } else {
            println("error")
            // The network was inaccessible and we have no cached data for
            // this query.
        }
    })

    println(latArray)
    println("^^Latitude values")
    println(longArray)
    println("^^Longitude values")
    println(addressArray)
    println("^^Address values")
}

1 Answer 1

1

The query is being executed in the background. The code in the block (closure) is executed when the query is finished. Note: this might be and most likely is after viewDidAppear finishes. If you put your print statements after your for loop, you should see values. If you uncomment your reloadData method, the table should be updated with new information when the query finishes.

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

Comments

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.