0

Could anyone tell me why my startingPoints array is still at 0 elements? I know that I am getting objects returned during the query, because that print statement prints out each query result, however it seems like those objects are not getting appended to my local array. I've included the code snippet below...

func buildStartSpots() -> Void {
    let queryStartingPoints = PFQuery(className: "CarpoolSpots")
    queryStartingPoints.whereKey("spotCityIndex", equalTo: self.startingCity)
    queryStartingPoints.findObjectsInBackgroundWithBlock{(objects: [PFObject]?, error: NSError?) -> Void in
        if error == nil {
              for object in objects! {
                  print("starting point: \(object)")
                  self.startingPoints.append(object)
              }
        } else {
            // Log details of the failure
            print("Error: \(error!) \(error!.userInfo)")
        }
    }
    print("starting points")
    dump(self.startingPoints)
}
3
  • 3
    so what do the logs tell you? That the method findObjectsInBackgroundWithBlock is executed in the ... background! What exactly is your issue / problem here? Commented Jan 11, 2016 at 22:20
  • as @luk2302 points out, you are printing before the fetch completion block is executed: the object are being added, but on a different thread. print the array within the completion block and you should see them. Commented Jan 11, 2016 at 22:34
  • so, where do i append the contents from that query result to my startingPoints array, such that the elements from the startingPoints array are accessible outside of the enclosure? the funny thing is, i have the same exact implementation for a different type of query (one involving PFUsers), and it works as wanted, ie. the array elements are accessible outside of the enclosure, despite the "append" happening within an enclosure... Commented Jan 12, 2016 at 6:19

3 Answers 3

1

While I have no experience in Parse, the block is asynchronously executed and likely non-blocking as dictated by the method name of the API call. Therefore, it is not guaranteed that the data would be available at the time you call dump, since the background thread might still be doing its work.

The only place that the data is guaranteed to be available at is the completion block you supplied to the API call. So you might need some ways to notify changes to others, e.g. post an NSNotification or use event stream constructs from third party libraries (e.g. ReactiveCocoa, RxSwift).

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

1 Comment

It is pretty much guaranteed that the data is not present when OP calls dump because the request certainly takes some time.
0

When you try to access the array, you need to use it within the closure:

func buildStartSpots() -> Void {
    let queryStartingPoints = PFQuery(className: "CarpoolSpots")
    queryStartingPoints.whereKey("spotCityIndex", equalTo: self.startingCity)
    queryStartingPoints.findObjectsInBackgroundWithBlock{(objects: [PFObject]?, error: NSError?) -> Void in
        if error == nil {
              for object in objects! {
                  print("starting point: \(object)")
                  **self.startingPoints.append(object)**
              }

//use it here
startingPoints xxx
        } else {
            // Log details of the failure
            print("Error: \(error!) \(error!.userInfo)")
        }
    }
    print("starting points")
    dump(self.startingPoints)
}

2 Comments

how do i make it so that the elements of that array are accessible outside of the enclosure? because the results returned by that query will need to be used by another function in that class.
call the function on startingPoints inside the closure: method(startingPoints)
0

I am able to get the application functioning as intended and will close this answer out.

It seems as though that the startingPoints array is not empty, and the values I need can be accessed from a different function within that same class.

The code snippet I am using to access my locally stored query results array is here:

for object in self.startingPoints {

                let startingLat = object["spotLatitude"] as! Double
                let startingLong = object["spotLongitude"] as! Double
                let carpoolSpotAnnotation = CarpoolSpot(name: object.valueForKey("spotTitle") as! String, subTitle: object.valueForKey("spotSubtitle") as! String, coordinate: CLLocationCoordinate2D(latitude: startingLat, longitude: startingLong))
                self.mapView.addAnnotation(carpoolSpotAnnotation)

The code snippet above is located within my didUpdateLocations implementation of the locationManager function, and with this code, I am able to access the query results I need.

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.