1

I want to use CLGeocoder and always produce a CLLocation, whether the geocode call fails or not.

Unfortunately, you can't get a value to always without capturing an outside variable, and putting recover directly after the geocode call requires that recover returns either a CLPlacemark or Promise<CLPlacemark>, which requires that I unnecessarily create a CLPlacemark just to produce a default CLLocation that I already have.

// build error because recover's return type must be the same as geocode's
CLGeocoder().geocode(postalCode).recover { error -> CLLocation in

Currently, I have this workaround:

CLGeocoder().geocode(postalCode)
                    .then { placemark -> CLLocation in
                        guard let location = placemark.location else {
                            throw GeocodeError
                        }
                        return location
                    }
                    .recover { error -> CLLocation in
                        // ... make a default CLLocation
                        return defaultLocation
                    }
                    .then { location -> Void in // this is like an always
                        // handle location
                    }

Is there a better way to achieve what I want?

Is recover guaranteed to be called if the geocode promise fails but is not chained directly after it?

ray
  • 1,966
  • 4
  • 24
  • 39

0 Answers0