简体   繁体   中英

Parse query results aren't being added (appended) to a local array in iOS Swift

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

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, eg post an NSNotification or use event stream constructs from third party libraries (eg ReactiveCocoa, RxSwift).

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM