0

I know this is probably a simple queston, I would like to return the value of currentLocGeoPoint and return the array of Objects which is of type PFObject.

  1. Tried to save it as a global variable, but it doesn't work because it is asynchronous and doesn't take a value yet. Returns empty.
  2. Tried to return currentLocGeoPoint and changed Void in to PFGeoPoint in. Gives error: PFGeoPoint is not convertible to 'Void'

So I'm not sure how I can fetch the variable currentLocGeoPoint.

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: { (placemarks, error) -> Void in
        if (error != nil) {
            println("Error:" + error.localizedDescription)
            //return
        }
        if placemarks.count > 0 {
            let pm = placemarks[0] as CLPlacemark
            self.displayLocationInfo(pm)
            currentLoc = manager.location 
            currentLocGeoPoint = PFGeoPoint(location:currentLoc)
            var query = PFQuery(className:"Bar") 
            query.whereKey("BarLocation", nearGeoPoint:currentLocGeoPoint, withinMiles:10) 
            query.limit = 500
            query.findObjectsInBackgroundWithBlock {
                (objects: [AnyObject]!, error: NSError!) -> Void in
                if objects != nil {  
                } else {
                    println("error: \(error)")
                }
            }
        } else {
            println("error: \(error)")
        }
    })
}
6
  • The code you posted will not compile. Commented Dec 31, 2014 at 13:21
  • @Kreiri i updated with the omitted code Commented Dec 31, 2014 at 13:28
  • Return value for function inside a block Commented Dec 31, 2014 at 13:32
  • @Kreiri thanks for the heads up. is there a swift example, as I couldn't find one anywhere and had trouble implementing into my code :\ Commented Dec 31, 2014 at 13:47
  • @Kreiri if the current code i have already has a completionhandler, would I be able to add another as suggested in your linked solution? Commented Dec 31, 2014 at 14:15

2 Answers 2

1

I don't understand the notion of "I want to return currentLocGeoPoint". Return it to what? You're in a CLLocationManagerDelegate method, so there's no one to return it to.

What you could do, though, is, when the request is done (i.e. within this closure), call some other function that needed the currentLocGeoPoint. Or you could update the UI to reflect the updated information (make sure to dispatch that update to the main thread, though). Or, if you have other view controllers or model objects that need to know about the new data, you might post a notification, letting them know that there is an updated currentLocGeoPoint. But within this method, there's no one to whom you would "return" the data.

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

2 Comments

Yes, I have tried calling the other functions that need the currentLocGeoPoint. I used the GeoPoint to create an array, but eventually, I would need to pass the value (of the array) back to the main thread and also to pass to the next viewController.
So, just have the completion closure set whatever properties you want. Just have it call whatever methods you want. If you want second view controller know about it, have the property in that other view controller implement a didSet clause in the property. Or observe a notification. Whatever you want.
0

You could assign it to a stored property of your class. Just use

self.<property> = currentLocGeoPoint

2 Comments

That still doesn't seem to get around async function not having a value yet.
If you make that in the closure and that is the completion handler then it must have a value.

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.