Josh O'Connor
Josh O'Connor

Reputation: 4962

Query class in Parse (swift)

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")
}

Upvotes: 0

Views: 95

Answers (1)

Eric
Eric

Reputation: 2097

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.

Upvotes: 1

Related Questions