uchicago-mobi / 2016-Winter-Forum

Class Forum for MPCS51030.
3 stars 0 forks source link

Geocode before submit #229

Closed lw334 closed 8 years ago

lw334 commented 8 years ago

I'm trying to let the user specify a place by address and geocode it before I send the form to the server. But the geocoding is async and ends up sending empty locations. What is the best way to handle this? Thanks a lot!

ghost commented 8 years ago

I still don't entirely comprehend GCD, but what I'd try is: dispatch_async for geocoding, followed by dispatch_after (a reasonable amount of time) to send the form to the server, both on the main queue. This way, your program waits long enough for the network request to complete, and then can gracefully handle a nil value or an error.

susanstevens commented 8 years ago

@lw334 All of the geocoding functions come with completion handlers that will be executed only after the geocoding is complete.

let addressString = "123 Some Street, Chicago, IL"
geocoder.geocodeAddressString(addressString, completionHandler: { placemarks, error in
    // code you want to execute after geocoder does its thing
})