I'm trying to find the distance in miles between two PFGeoPoints; the user's current location and a saved location in Parse. In my code, I'm retrieving the saved location and storing it in itemLocation.
Immediately after, I am finding the user's current location using geoPointForCurrentLocationInBackground and comparing the distance between the two with distanceInMiles and assigning the value to the variable named distance.
After saving all this, the "distance" field in Parse still comes up blank so it's not saving. What am I doing wrong here?
func retrieveItemInfo() {
var items = PFObject(className: "Items")
var query = PFQuery(className: "Items")
query.orderByDescending("createdAt")
query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = objects {
for (index, title) in enumerate(objects) {
let itemLocation = (title as! PFObject)["location"] as! PFGeoPoint
PFGeoPoint.geoPointForCurrentLocationInBackground({ (geoPoint: PFGeoPoint?, error: NSError?) -> Void in
if error == nil {
var userLocation = geoPoint
var distance = userLocation?.distanceInMilesTo(itemLocation)
items.setValue(distance, forKey: "distance")
items.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
if success {
} else {
println("Error")
}
})
}
})